Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,12 @@ private StorageEngineMessages() {}
public static final String STORAGE_LOG_DEGRADE_LASTFLUSHTIMEMAP_OF_OLD_TIMEPARTITIONINFO_MEM_SIZE_BED053EE =
"[{}]degrade LastFlushTimeMap of old TimePartitionInfo-{}, mem size is {}, remaining mem "
+ "cost is {}";
public static final String STORAGE_LOG_FAILED_TO_DEGRADE_LASTFLUSHTIMEMAP_OF_2B2924D0 =
"[{}]Failed to degrade LastFlushTimeMap of TimePartitionInfo-{}, keep it registered so "
+ "that its memory is still accounted";
public static final String STORAGE_LOG_FAILED_TO_DEGRADE_LASTFLUSHTIMEMAP_OF_REPLACED_47B56998 =
"[{}]Failed to degrade LastFlushTimeMap of replaced TimePartitionInfo-{}, keep its memory "
+ "accounted";
public static final String STORAGE_LOG_LIMIT_OF_ARRAY_DEQUE_SIZE_UPDATED_05DBA95E =
"limit of {} array deque size updated: {} -> {}";
public static final String STORAGE_LOG_LIMITUPDATETHRESHOLD_OF_PRIMITIVEARRAYMANAGER_UPDATED_394801AE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,10 @@ private StorageEngineMessages() {}
"SG ({}) 已释放内存 (delta:{}),但系统仍处于拒绝状态 (totalSgMemCost:{})。";
public static final String STORAGE_LOG_DEGRADE_LASTFLUSHTIMEMAP_OF_OLD_TIMEPARTITIONINFO_MEM_SIZE_BED053EE =
"[{}] 降级旧 TimePartitionInfo-{} 的 LastFlushTimeMap,内存大小为 {},剩余内存开销为 {}";
public static final String STORAGE_LOG_FAILED_TO_DEGRADE_LASTFLUSHTIMEMAP_OF_2B2924D0 =
"[{}] 降级 TimePartitionInfo-{} 的 LastFlushTimeMap 失败,保留该分区的注册信息以继续统计其内存";
public static final String STORAGE_LOG_FAILED_TO_DEGRADE_LASTFLUSHTIMEMAP_OF_REPLACED_47B56998 =
"[{}] 降级被替换的 TimePartitionInfo-{} 的 LastFlushTimeMap 失败,继续统计其内存";
public static final String STORAGE_LOG_LIMIT_OF_ARRAY_DEQUE_SIZE_UPDATED_05DBA95E =
"{} array deque 大小限制已更新:{} -> {}";
public static final String STORAGE_LOG_LIMITUPDATETHRESHOLD_OF_PRIMITIVEARRAYMANAGER_UPDATED_394801AE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public class DataRegion implements IDataRegionForQuery {
/** flush listeners. */
private List<FlushListener> customFlushListeners = Collections.emptyList();

private ILastFlushTimeMap lastFlushTimeMap;
private final ILastFlushTimeMap lastFlushTimeMap;

/**
* Record the insertWriteLock in SG is being hold by which method, it will be empty string if no
Expand Down Expand Up @@ -465,6 +465,11 @@ public DataRegion(
logger.error(StorageEngineMessages.CREATE_DB_SYSTEM_DIR_FAILED, dataRegionSysDir.getPath());
}

// Must be initialized before recover(): during recovery, `this` is published to
// TimePartitionManager through TimePartitionInfo (before this region is registered in
// StorageEngine), and other threads may call degradeFlushTimeMap() on it. TimePartitionManager
// synchronizes registration and degradation on the same monitor, and lastFlushTimeMap is final,
// so those threads always see a fully initialized map.
lastFlushTimeMap = new HashLastFlushTimeMap();
upgradeModFileThreadPool =
IoTDBThreadPoolFactory.newSingleThreadExecutor(
Expand Down Expand Up @@ -524,6 +529,7 @@ public DataRegion(String databaseName, String dataRegionIdString) {
this.dataRegionId = new DataRegionId(Integer.parseInt(this.dataRegionIdString));
this.dataRegionInfo = new DataRegionInfo(this);
this.tsFileManager = new TsFileManager(databaseName, dataRegionIdString, "");
this.lastFlushTimeMap = new HashLastFlushTimeMap();
this.partitionMaxFileVersions = new HashMap<>();
partitionMaxFileVersions.put(0L, 0L);
upgradeModFileThreadPool = null;
Expand Down Expand Up @@ -774,17 +780,6 @@ private void recover() throws DataRegionException {
WALRecoverManager.getInstance().getAllDataRegionScannedLatch().countDown();
// recover sealed TsFiles
if (!partitionTmpSeqTsFiles.isEmpty() || !partitionTmpUnseqTsFiles.isEmpty()) {
long latestPartitionId = Long.MIN_VALUE;
if (!partitionTmpSeqTsFiles.isEmpty()) {
latestPartitionId =
((TreeMap<Long, List<TsFileResource>>) partitionTmpSeqTsFiles).lastKey();
}
if (!partitionTmpUnseqTsFiles.isEmpty()) {
latestPartitionId =
Math.max(
latestPartitionId,
((TreeMap<Long, List<TsFileResource>>) partitionTmpUnseqTsFiles).lastKey());
}
File logFile = SystemFileFactory.INSTANCE.getFile(dataRegionSysDir, "FileTimeIndexCache_0");
Map<TsFileID, FileTimeIndex> fileTimeIndexMap = new HashMap<>();
if (logFile.exists()) {
Expand Down Expand Up @@ -821,16 +816,6 @@ private void recover() throws DataRegionException {
asyncTsFileResourceRecoverTaskList.add(asyncRecoverTask);
}
}
if (config.isEnableSeparateData()) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(
new DataRegionId(Integer.parseInt(dataRegionIdString)),
latestPartitionId,
false,
Long.MAX_VALUE,
lastFlushTimeMap.getMemSize(latestPartitionId)));
}
}
// wait until all unsealed TsFiles have been recovered
for (WALRecoverListener recoverListener : recoverListeners) {
Expand Down Expand Up @@ -904,19 +889,74 @@ protected void updateDeviceLastFlushTime(TsFileResource resource) {
}
}

private void updateAndRegisterDeviceLastFlushTime(TsFileResource resource) {
if (!config.isEnableSeparateData()) {
return;
}
long timePartitionId = resource.getTimePartition();
if (!lastFlushTimeMap.checkAndCreateFlushedTimePartition(timePartitionId, true)) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(dataRegionId, timePartitionId, false, Long.MAX_VALUE, 0, this));
}
updateDeviceLastFlushTime(resource);
TimePartitionManager.getInstance()
.updateAfterFlushing(
dataRegionId,
timePartitionId,
System.currentTimeMillis(),
lastFlushTimeMap.getMemSize(timePartitionId),
false);
}

protected void upgradeAndUpdateDeviceLastFlushTime(
long timePartitionId, List<TsFileResource> resources) {
if (!config.isEnableSeparateData()) {
return;
}
if (lastFlushTimeMap.isPartitionLastFlushTime(timePartitionId)) {
updatePartitionMaxEndTime(timePartitionId, resources);
return;
}
Map<IDeviceID, Long> endTimeMap = new HashMap<>();
for (TsFileResource resource : resources) {
if (resource.isDeleted()) {
continue;
}
for (IDeviceID deviceId : resource.getDevices()) {
// checked above
//noinspection OptionalGetWithoutIsPresent
long endTime = resource.getEndTime(deviceId).get();
endTimeMap.merge(deviceId, endTime, Math::max);
}
}
if (config.isEnableSeparateData()) {
if (endTimeMap.isEmpty()) {
return;
}
writeLock("upgradeAndUpdateDeviceLastFlushTime");
try {
lastFlushTimeMap.upgradeAndUpdateMultiDeviceFlushedTime(timePartitionId, endTimeMap);
TimePartitionManager.getInstance()
.updateAfterFlushing(
dataRegionId,
timePartitionId,
System.currentTimeMillis(),
lastFlushTimeMap.getMemSize(timePartitionId),
workSequenceTsFileProcessors.get(timePartitionId) != null);
} finally {
writeUnlock();
}
}

private void updatePartitionMaxEndTime(long timePartitionId, List<TsFileResource> resources) {
long maxEndTime = Long.MIN_VALUE;
for (TsFileResource resource : resources) {
if (!resource.isDeleted() && resource.getTimeIndex() != null) {
maxEndTime = Math.max(maxEndTime, resource.getTimeIndex().getMaxEndTime());
}
}
if (maxEndTime != Long.MIN_VALUE) {
lastFlushTimeMap.updatePartitionFlushedTime(timePartitionId, maxEndTime);
}
}

Expand Down Expand Up @@ -1076,16 +1116,16 @@ private void recoverUnsealedTsFileCallBack(UnsealedTsFileRecoverPerformer recove
tsFileResource.remove();
return;
}
updateDeviceLastFlushTime(tsFileResource);
updateAndRegisterDeviceLastFlushTime(tsFileResource);
tsFileResourceManager.registerSealedTsFileResource(tsFileResource);
FileMetrics.getInstance().addTsFile(tsFileResource);
} else {
// the last file is not closed, continue writing to it
RestorableTsFileIOWriter writer = recoverPerformer.getWriter();
long timePartitionId = tsFileResource.getTimePartition();
initFlushTimeMap(timePartitionId);
TimePartitionManager.getInstance()
.updateAfterOpeningTsFileProcessor(
new DataRegionId(Integer.parseInt(dataRegionIdString)), timePartitionId);
.updateAfterOpeningTsFileProcessor(dataRegionId, timePartitionId);
TsFileProcessor tsFileProcessor =
new TsFileProcessor(
dataRegionIdString,
Expand Down Expand Up @@ -1193,19 +1233,14 @@ private Callable<Void> asyncRecoverFilesInPartition(
if (!lastFlushTimeMap.checkAndCreateFlushedTimePartition(partitionId, false)) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(
new DataRegionId(Integer.parseInt(dataRegionIdString)),
partitionId,
false,
Long.MAX_VALUE,
lastFlushTimeMap.getMemSize(partitionId)));
new TimePartitionInfo(dataRegionId, partitionId, false, Long.MAX_VALUE, 0, this));
}
for (TsFileResource tsFileResource : resourceList) {
updatePartitionLastFlushTime(tsFileResource);
}
TimePartitionManager.getInstance()
.updateAfterFlushing(
new DataRegionId(Integer.parseInt(dataRegionIdString)),
dataRegionId,
partitionId,
System.currentTimeMillis(),
lastFlushTimeMap.getMemSize(partitionId),
Expand Down Expand Up @@ -1253,12 +1288,7 @@ private void syncRecoverFilesInPartition(
if (!lastFlushTimeMap.checkAndCreateFlushedTimePartition(partitionId, true)) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(
new DataRegionId(Integer.parseInt(dataRegionIdString)),
partitionId,
false,
Long.MAX_VALUE,
lastFlushTimeMap.getMemSize(partitionId)));
new TimePartitionInfo(dataRegionId, partitionId, false, Long.MAX_VALUE, 0, this));
}
for (TsFileResource tsFileResource : resourceList) {
if (!tsFileResource.isDeleted()) {
Expand All @@ -1267,7 +1297,7 @@ private void syncRecoverFilesInPartition(
}
TimePartitionManager.getInstance()
.updateAfterFlushing(
new DataRegionId(Integer.parseInt(dataRegionIdString)),
dataRegionId,
partitionId,
System.currentTimeMillis(),
lastFlushTimeMap.getMemSize(partitionId),
Expand Down Expand Up @@ -1694,7 +1724,8 @@ private void initFlushTimeMap(long timePartitionId) {
timePartitionId,
true,
Long.MAX_VALUE,
0));
0,
this));
}
}

Expand Down Expand Up @@ -4786,29 +4817,7 @@ public void loadNewTsFile(

FileMetrics.getInstance().addTsFile(newTsFileResource);

if (config.isEnableSeparateData()) {
final DataRegionId dataRegionId =
new DataRegionId(Integer.parseInt(this.dataRegionIdString));
final long timePartitionId = newTsFileResource.getTimePartition();
if (!lastFlushTimeMap.checkAndCreateFlushedTimePartition(timePartitionId, true)) {
TimePartitionManager.getInstance()
.registerTimePartitionInfo(
new TimePartitionInfo(
dataRegionId,
timePartitionId,
false,
Long.MAX_VALUE,
lastFlushTimeMap.getMemSize(timePartitionId)));
}
updateDeviceLastFlushTime(newTsFileResource);
TimePartitionManager.getInstance()
.updateAfterFlushing(
dataRegionId,
timePartitionId,
System.currentTimeMillis(),
lastFlushTimeMap.getMemSize(timePartitionId),
false);
}
updateAndRegisterDeviceLastFlushTime(newTsFileResource);

onTsFileLoaded(newTsFileResource, isFromConsensus, lastValues, lastReader);
if (isGeneratedByPipe) {
Expand Down Expand Up @@ -5364,7 +5373,8 @@ public void insert(InsertRowsOfOneDeviceNode insertRowsOfOneDeviceNode)
timePartitionId,
true,
Long.MAX_VALUE,
0));
0,
this));
}

boolean isSequence =
Expand Down Expand Up @@ -5489,7 +5499,8 @@ public void insert(InsertRowsNode insertRowsNode)
timePartitionIds[i],
true,
Long.MAX_VALUE,
0));
0,
this));
}
areSequence[i] =
config.isEnableSeparateData()
Expand Down Expand Up @@ -5836,8 +5847,8 @@ private void releaseDirectBufferMemory() {
}

/* Be careful, the thread that calls this method may not hold the write lock!!*/
public void degradeFlushTimeMap(long timePartitionId) {
lastFlushTimeMap.degradeLastFlushTime(timePartitionId);
public boolean degradeFlushTimeMap(long timePartitionId) {
return lastFlushTimeMap.degradeLastFlushTime(timePartitionId);
}

public ResidentSeriesCounter getResidentSeriesCounter() {
Expand Down
Loading
Loading