Repository navigation
Fix LastFlushTimeMap degradation skipped during DataRegion recovery #19545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bbae646
18d6d07
fe13f4c
b851162
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,104 +44,123 @@ public class HashLastFlushTimeMap implements ILastFlushTimeMap { | |
| /** record memory cost of map for each partitionId */ | ||
| private final Map<Long, Long> memCostForEachPartition = new ConcurrentHashMap<>(); | ||
|
|
||
| /* | ||
| * Concurrency note: every mutation of an ILastFlushTime (and of the memory cost of its partition) | ||
| * is performed inside partitionLatestFlushedTime.compute(partitionId, ...). The degradation | ||
| * triggered by TimePartitionManager may run in another thread (e.g. the recovery thread of | ||
| * another DataRegion, or a flush thread that does not hold this region's write lock), and it | ||
| * replaces the ILastFlushTime inside computeIfPresent(partitionId, ...). Serializing both on the | ||
| * same key guarantees that the degradation never iterates a DeviceLastFlushTime that is being | ||
| * modified, and that no update is applied to a stale DeviceLastFlushTime after it was replaced. | ||
| */ | ||
|
|
||
| // For sync recover resource without fileTimeIndexCache and load | ||
| @Override | ||
| public void updateMultiDeviceFlushedTime( | ||
| long timePartitionId, Map<IDeviceID, Long> flushedTimeMap) { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| partitionLatestFlushedTime.computeIfAbsent( | ||
| timePartitionId, id -> new DeviceLastFlushTime()); | ||
|
|
||
| long memIncr = 0L; | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| if (flushTimeMapForPartition.getLastFlushTime(entry.getKey()) == Long.MIN_VALUE) { | ||
| memIncr += HASHMAP_NODE_BASIC_SIZE + entry.getKey().ramBytesUsed(); | ||
| } | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| long finalMemIncr = memIncr; | ||
| memCostForEachPartition.compute( | ||
| timePartitionId, (k1, v1) -> v1 == null ? finalMemIncr : v1 + finalMemIncr); | ||
| partitionLatestFlushedTime.compute( | ||
| timePartitionId, | ||
| (id, lastFlushTime) -> { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| lastFlushTime == null ? new DeviceLastFlushTime() : lastFlushTime; | ||
| // a degraded partition only keeps one long, so device entries cost no extra memory | ||
| boolean isDeviceLevel = flushTimeMapForPartition instanceof DeviceLastFlushTime; | ||
| long memIncr = 0L; | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| if (isDeviceLevel | ||
| && flushTimeMapForPartition.getLastFlushTime(entry.getKey()) == Long.MIN_VALUE) { | ||
| memIncr += HASHMAP_NODE_BASIC_SIZE + entry.getKey().ramBytesUsed(); | ||
| } | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| memCostForEachPartition.merge(id, memIncr, Long::sum); | ||
| return flushTimeMapForPartition; | ||
| }); | ||
| } | ||
|
|
||
| // For async recover resource with fileTimeIndexCache | ||
| @Override | ||
| public void upgradeAndUpdateMultiDeviceFlushedTime( | ||
| long timePartitionId, Map<IDeviceID, Long> flushedTimeMap) { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| partitionLatestFlushedTime.computeIfAbsent( | ||
| timePartitionId, id -> new DeviceLastFlushTime()); | ||
| // upgrade DeviceLastFlushTime to PartitionLastFlushTime | ||
| if (flushTimeMapForPartition instanceof PartitionLastFlushTime) { | ||
| long maxFlushTime = flushTimeMapForPartition.getLastFlushTime(null); | ||
| ILastFlushTime newDeviceLastFlushTime = new DeviceLastFlushTime(); | ||
| long memIncr = 0; | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| memIncr += HASHMAP_NODE_BASIC_SIZE + entry.getKey().ramBytesUsed(); | ||
| newDeviceLastFlushTime.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| maxFlushTime = Math.max(maxFlushTime, entry.getValue()); | ||
| } | ||
| long finalMemIncr = memIncr; | ||
| memCostForEachPartition.compute( | ||
| timePartitionId, (k1, v1) -> v1 == null ? finalMemIncr : v1 + finalMemIncr); | ||
| } else { | ||
| // go here when DeviceLastFlushTime was recovered by wal recovery | ||
| long memIncr = 0; | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| if (flushTimeMapForPartition.getLastFlushTime(entry.getKey()) == Long.MIN_VALUE) { | ||
| memIncr += HASHMAP_NODE_BASIC_SIZE + entry.getKey().ramBytesUsed(); | ||
| } | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| long finalMemIncr = memIncr; | ||
| memCostForEachPartition.compute( | ||
| timePartitionId, (k1, v1) -> v1 == null ? finalMemIncr : v1 + finalMemIncr); | ||
| } | ||
| partitionLatestFlushedTime.compute( | ||
| timePartitionId, | ||
| (id, lastFlushTime) -> { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| lastFlushTime == null ? new DeviceLastFlushTime() : lastFlushTime; | ||
| if (flushTimeMapForPartition instanceof PartitionLastFlushTime) { | ||
| // The partition only keeps a partition-level flush time, either because it was | ||
| // recovered from the FileTimeIndexCache, or because TimePartitionManager has degraded | ||
| // it to release memory. Keep it degraded, but merge the recovered maximum into it so | ||
| // that the partition flush time is never lower than the real max end time. | ||
| for (long flushedTime : flushedTimeMap.values()) { | ||
| flushTimeMapForPartition.updateLastFlushTime(null, flushedTime); | ||
| } | ||
| } else { | ||
| // go here when DeviceLastFlushTime was recovered by wal recovery | ||
| long memIncr = 0; | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| if (flushTimeMapForPartition.getLastFlushTime(entry.getKey()) == Long.MIN_VALUE) { | ||
| memIncr += HASHMAP_NODE_BASIC_SIZE + entry.getKey().ramBytesUsed(); | ||
| } | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| memCostForEachPartition.merge(id, memIncr, Long::sum); | ||
| } | ||
| return flushTimeMapForPartition; | ||
| }); | ||
| } | ||
|
|
||
| // For fileTimeIndexCache recovered before the async resource recover start | ||
| @Override | ||
| public void updatePartitionFlushedTime(long timePartitionId, long maxFlushedTime) { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| partitionLatestFlushedTime.computeIfAbsent( | ||
| timePartitionId, id -> new PartitionLastFlushTime(maxFlushedTime)); | ||
|
|
||
| if (flushTimeMapForPartition instanceof PartitionLastFlushTime) { | ||
| long memIncr = Long.BYTES; | ||
| flushTimeMapForPartition.updateLastFlushTime(null, maxFlushedTime); | ||
| memCostForEachPartition.putIfAbsent(timePartitionId, memIncr); | ||
| } else { | ||
| // go here when DeviceLastFlushTime was recovered by wal recovery | ||
| DeviceLastFlushTime deviceLastFlushTime = (DeviceLastFlushTime) flushTimeMapForPartition; | ||
| Map<IDeviceID, Long> flushedTimeMap = deviceLastFlushTime.getDeviceLastFlushTimeMap(); | ||
| for (Map.Entry<IDeviceID, Long> entry : flushedTimeMap.entrySet()) { | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| } | ||
| partitionLatestFlushedTime.compute( | ||
| timePartitionId, | ||
| (id, lastFlushTime) -> { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| lastFlushTime == null ? new PartitionLastFlushTime(maxFlushedTime) : lastFlushTime; | ||
| if (flushTimeMapForPartition instanceof PartitionLastFlushTime) { | ||
| flushTimeMapForPartition.updateLastFlushTime(null, maxFlushedTime); | ||
| memCostForEachPartition.putIfAbsent(id, (long) Long.BYTES); | ||
| } | ||
| // Otherwise the DeviceLastFlushTime was recovered by wal recovery or sync recovery, and | ||
| // the device flush times of these resources are merged into it by | ||
| // upgradeAndUpdateMultiDeviceFlushedTime after the async recovery. | ||
| return flushTimeMapForPartition; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean checkAndCreateFlushedTimePartition( | ||
| long timePartitionId, boolean usingDeviceFlushTime) { | ||
| if (!partitionLatestFlushedTime.containsKey(timePartitionId)) { | ||
| partitionLatestFlushedTime.put( | ||
| timePartitionId, | ||
| usingDeviceFlushTime | ||
| ? new DeviceLastFlushTime() | ||
| : new PartitionLastFlushTime(Long.MIN_VALUE)); | ||
| return false; | ||
| // fast path without allocation, this method is called for every insertion | ||
| if (partitionLatestFlushedTime.containsKey(timePartitionId)) { | ||
| return true; | ||
| } | ||
| return true; | ||
| // putIfAbsent keeps the creation atomic when several threads meet a new partition together | ||
| return partitionLatestFlushedTime.putIfAbsent( | ||
| timePartitionId, | ||
| usingDeviceFlushTime | ||
| ? new DeviceLastFlushTime() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Avoid allocating a device map on every partition hit
Please return early when the key already exists, then construct the value and use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in fe13f4c: |
||
| : new PartitionLastFlushTime(Long.MIN_VALUE)) | ||
| != null; | ||
| } | ||
|
|
||
| // For insert | ||
| @Override | ||
| public void updateLatestFlushTime(long partitionId, Map<IDeviceID, Long> updateMap) { | ||
| for (Map.Entry<IDeviceID, Long> entry : updateMap.entrySet()) { | ||
| partitionLatestFlushedTime | ||
| .computeIfAbsent(partitionId, id -> new DeviceLastFlushTime()) | ||
| .updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| if (updateMap.isEmpty()) { | ||
| return; | ||
| } | ||
| partitionLatestFlushedTime.compute( | ||
| partitionId, | ||
| (id, lastFlushTime) -> { | ||
| ILastFlushTime flushTimeMapForPartition = | ||
| lastFlushTime == null ? new DeviceLastFlushTime() : lastFlushTime; | ||
| for (Map.Entry<IDeviceID, Long> entry : updateMap.entrySet()) { | ||
| flushTimeMapForPartition.updateLastFlushTime(entry.getKey(), entry.getValue()); | ||
| } | ||
| return flushTimeMapForPartition; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -158,8 +177,12 @@ public void clearFlushedTime() { | |
| @Override | ||
| public void degradeLastFlushTime(long partitionId) { | ||
| partitionLatestFlushedTime.computeIfPresent( | ||
| partitionId, (id, lastFlushTime) -> lastFlushTime.degradeLastFlushTime()); | ||
| memCostForEachPartition.put(partitionId, (long) Long.BYTES); | ||
| partitionId, | ||
| (id, lastFlushTime) -> { | ||
| ILastFlushTime degradedLastFlushTime = lastFlushTime.degradeLastFlushTime(); | ||
| memCostForEachPartition.put(id, (long) Long.BYTES); | ||
| return degradedLastFlushTime; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,10 @@ | |
| package org.apache.iotdb.db.storageengine.rescon.memory; | ||
|
|
||
| import org.apache.iotdb.commons.consensus.DataRegionId; | ||
| import org.apache.iotdb.commons.utils.TestOnly; | ||
| import org.apache.iotdb.db.storageengine.dataregion.DataRegion; | ||
|
|
||
| import java.lang.ref.WeakReference; | ||
|
|
||
| /** Time partition info records necessary info of a time partition for a data region */ | ||
| public class TimePartitionInfo { | ||
|
|
@@ -33,17 +37,45 @@ public class TimePartitionInfo { | |
|
|
||
| long memSize; | ||
|
|
||
| /** | ||
| * The DataRegion that owns this time partition. It is used to degrade the LastFlushTimeMap of the | ||
| * region when the region is not (or not yet) the one registered in StorageEngine, e.g. while the | ||
| * region is being constructed and recovered. A weak reference is used so that a region whose | ||
| * construction failed is not pinned in memory by TimePartitionManager. | ||
| */ | ||
| private final WeakReference<DataRegion> dataRegionRef; | ||
|
|
||
| /** | ||
| * Production code should always pass the owning DataRegion, otherwise the LastFlushTimeMap of a | ||
| * region that is not registered in StorageEngine cannot be degraded. | ||
| */ | ||
| @TestOnly | ||
| public TimePartitionInfo( | ||
| DataRegionId dataRegionId, | ||
| long partitionId, | ||
| boolean isActive, | ||
| long lastSystemFlushTime, | ||
| long memsize) { | ||
| this(dataRegionId, partitionId, isActive, lastSystemFlushTime, memsize, null); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: after this PR, no main code uses the 5-argument constructor anymore (all call sites in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marked the 5-argument constructor |
||
| } | ||
|
|
||
| public TimePartitionInfo( | ||
| DataRegionId dataRegionId, | ||
| long partitionId, | ||
| boolean isActive, | ||
| long lastSystemFlushTime, | ||
| long memsize, | ||
| DataRegion dataRegion) { | ||
| this.dataRegionId = dataRegionId; | ||
| this.partitionId = partitionId; | ||
| this.isActive = isActive; | ||
| this.lastSystemFlushTime = lastSystemFlushTime; | ||
| this.memSize = memsize; | ||
| this.dataRegionRef = dataRegion == null ? null : new WeakReference<>(dataRegion); | ||
| } | ||
|
|
||
| DataRegion getDataRegion() { | ||
| return dataRegionRef == null ? null : dataRegionRef.get(); | ||
| } | ||
|
|
||
| public int comparePriority(TimePartitionInfo timePartitionInfo) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concurrency concern introduced by making recovering regions degradable.
Here the partition is registered (with
memSize= 0) before the loop below populates itsDeviceLastFlushTime(a plainHashMap). Data regions are recovered in parallel, so with this change the following interleaving becomes possible:Possible outcomes:
ConcurrentModificationExceptionthrown in thread A.recover()only catchesIOException, so A's region recovery fails, andTimePartitionManageris left withrelease()done but the entry not removed.DaftercomputeIfPresenthas swapped in aPartitionLastFlushTime. The degraded partition flush time is then lower than the real max end time, so later writes in(degraded, real]may be classified as sequence, which can produce overlapping seq files.In-progress partitions (memSize 0, small partitionId) are actually the first candidates in
comparePriority, and degrading them frees nothing, so this window is easy to hit in exactly the memory-pressure scenario this PR targets. A single-region Edge setup does not exercise it.Suggestions:
recoverUnsealedTsFileCallBack->updateDeviceLastFlushTime, or theDeviceLastFlushTimebranch ofupgradeAndUpdateMultiDeviceFlushedTimein the async tasks, which mutate already-registered partitions.HashLastFlushTimeMap, perform every mutation of anILastFlushTimeinsidepartitionLatestFlushedTime.compute(partitionId, ...), so it is serialized per partition withdegradeLastFlushTime'scomputeIfPresent. This would also close the pre-existing runtime race noted ondegradeFlushTimeMap("may not hold the write lock").There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 18d6d07 with option (b): every mutation of an
ILastFlushTimeinHashLastFlushTimeMap(updateMultiDeviceFlushedTime,upgradeAndUpdateMultiDeviceFlushedTime,updatePartitionFlushedTime,updateLatestFlushTime) and its memory-cost update now run insidepartitionLatestFlushedTime.compute(partitionId, ...).degradeLastFlushTimeruns insidecomputeIfPresentand resets the memory cost there too. Degradation can no longer iterate aDeviceLastFlushTimethat is being modified, and no update goes into a staleDeviceLastFlushTimeafter it is replaced. Later updates go into thePartitionLastFlushTimeand are merged by max. This also coversrecoverUnsealedTsFileCallBackand the async upgrade path, plus the runtime "may not hold the write lock" race.checkAndCreateFlushedTimePartitionnow usesputIfAbsent. Also, a degraded partition no longer gains phantom memory cost from later device updates. New test:LastFlushTimeMapTest#testConcurrentUpdateAndDegrade, which fails on the previous commit withConcurrentModificationException.