Skip to content

Commit

Permalink
add FileSizeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
nginthfs committed Sep 16, 2024
1 parent c549b75 commit 4737575
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions solr/core/src/java/org/apache/solr/storage/SizeAwareDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SizeAwareDirectory extends FilterDirectory
this.onDiskSize.add(onDiskSize);
};

private final ConcurrentHashMap<String, Sizes> fileSizeMap = new ConcurrentHashMap<>();
private final FileSizeMap fileSizeMap = new FileSizeMap();

private final ConcurrentHashMap<String, SizeAccountingIndexOutput> liveOutputs =
new ConcurrentHashMap<>();
Expand All @@ -68,6 +68,44 @@ private interface SizeWriter {
void apply(long size, long onDiskSize, String name);
}

private static class FileSizeMap implements Accountable {
private final long RAM_BYTES_USED =
RamUsageEstimator.shallowSizeOfInstance(FileSizeMap.class);
private final ConcurrentHashMap<String, Sizes> fileSizeMap = new ConcurrentHashMap<>();
private volatile LongAdder onDiskSize = new LongAdder();

public Sizes add(String name, Sizes sizes) {
Sizes extant = fileSizeMap.put(name, sizes);
onDiskSize.add(sizes.onDiskSize);
return extant;
}

public Sizes remove(String name) {
Sizes sizes = fileSizeMap.remove(name);
if (sizes != null) {
onDiskSize.add(-sizes.onDiskSize);
}
return sizes;
}

public long onDiskSize() {
return onDiskSize.sum();
}

public Sizes get(String key) {
return fileSizeMap.get(key);
}

public long size() {
return fileSizeMap.size();
}

@Override
public long ramBytesUsed() {
return RAM_BYTES_USED;
}
}

private static class Sizes implements Accountable {
private static final long RAM_BYTES_USED =
RamUsageEstimator.shallowSizeOfInstance(SizeAccountingIndexOutput.class);
Expand Down Expand Up @@ -99,7 +137,7 @@ public SizeAwareDirectory(Directory in, long reconcileTTLNanos) {
@Override
public long ramBytesUsed() {
return BASE_RAM_BYTES_USED
+ RamUsageEstimator.sizeOfMap(fileSizeMap)
+ fileSizeMap.ramBytesUsed()
+ RamUsageEstimator.sizeOfMap(liveOutputs);
}

Expand Down Expand Up @@ -154,7 +192,7 @@ public long onDiskSize() throws IOException {
if (initialized
&& (reconcileThreshold == null
|| System.nanoTime() - reconciledTimeNanos < reconcileTTLNanos)) {
return onDiskSize.sum();
return fileSizeMap.onDiskSize();
}
return initSize().onDiskSize;
}
Expand Down Expand Up @@ -211,7 +249,7 @@ private Sizes initSize() throws IOException {
if (fileSize > 0) {
// whether the file exists or not, we don't care about it if it has zero size.
// more often though, 0 size means the file isn't there.
fileSizeMap.put(file, sizes);
fileSizeMap.add(file, sizes);
if (DirectoryFactory.sizeOf(in, file) == 0) {
// during reconciliation, we have to check for file presence _after_ adding
// to the map, to prevent a race condition that could leak entries into `fileSizeMap`
Expand Down Expand Up @@ -342,7 +380,7 @@ private static final class SizeAccountingIndexOutput extends IndexOutput impleme

private final IndexOutput backing;

private final ConcurrentHashMap<String, Sizes> fileSizeMap;
private final FileSizeMap fileSizeMap;

private final ConcurrentHashMap<String, SizeAccountingIndexOutput> liveOutputs;

Expand All @@ -353,7 +391,7 @@ private static final class SizeAccountingIndexOutput extends IndexOutput impleme
private SizeAccountingIndexOutput(
String name,
IndexOutput backing,
ConcurrentHashMap<String, Sizes> fileSizeMap,
FileSizeMap fileSizeMap,
ConcurrentHashMap<String, SizeAccountingIndexOutput> liveOutputs,
SizeWriter sizeWriter) {
super("byteSize(" + name + ")", name);
Expand Down Expand Up @@ -394,7 +432,7 @@ public void close() throws IOException {
// on-disk size here
sizeWriter.apply(0, finalBytesWritten - lastBytesWritten, name);
}
fileSizeMap.put(name, new Sizes(backing.getFilePointer(), onDiskSize));
fileSizeMap.add(name, new Sizes(backing.getFilePointer(), onDiskSize));
liveOutputs.remove(name);
}
}
Expand Down Expand Up @@ -442,7 +480,7 @@ public long ramBytesUsed() {
@Override
public void rename(String source, String dest) throws IOException {
in.rename(source, dest);
Sizes extant = fileSizeMap.put(dest, fileSizeMap.remove(source));
Sizes extant = fileSizeMap.add(dest, fileSizeMap.remove(source));
assert extant == null; // it's illegal for dest to already exist
}
}

0 comments on commit 4737575

Please sign in to comment.