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
Cache fileLength for fully written files #9683
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
54 changes: 0 additions & 54 deletions
54
src/main/java/org/elasticsearch/common/lucene/Directories.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,6 @@ | |
import org.elasticsearch.common.io.stream.Streamable; | ||
import org.elasticsearch.common.logging.ESLogger; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.common.lucene.Directories; | ||
import org.elasticsearch.common.lucene.Lucene; | ||
import org.elasticsearch.common.lucene.store.InputStreamIndexInput; | ||
import org.elasticsearch.common.settings.Settings; | ||
|
@@ -54,6 +53,7 @@ | |
import java.nio.file.NoSuchFileException; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.zip.Adler32; | ||
|
@@ -283,7 +283,7 @@ public void deleteContent() throws IOException { | |
|
||
public StoreStats stats() throws IOException { | ||
ensureOpen(); | ||
return new StoreStats(Directories.estimateSize(directory), directoryService.throttleTimeInNanos()); | ||
return new StoreStats(directory.estimatedSize(), directoryService.throttleTimeInNanos()); | ||
} | ||
|
||
public void renameFile(String from, String to) throws IOException { | ||
|
@@ -615,11 +615,20 @@ public int refCount() { | |
|
||
private static final class StoreDirectory extends FilterDirectory { | ||
|
||
/** | ||
* We cache the file length because we call fileLength(name) extensively | ||
* on out stats APIs. | ||
*/ | ||
private final ConcurrentHashMap<String, Long> fileLengthCache = new ConcurrentHashMap<>(); | ||
|
||
private final ESLogger deletesLogger; | ||
|
||
StoreDirectory(Directory delegateDirectory, ESLogger deletesLogger) throws IOException { | ||
super(delegateDirectory); | ||
this.deletesLogger = deletesLogger; | ||
for (String name : delegateDirectory.listAll()) { | ||
fileLengthCache.put(name, delegateDirectory.fileLength(name)); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -630,6 +639,7 @@ public void close() throws IOException { | |
public void deleteFile(String msg, String name) throws IOException { | ||
deletesLogger.trace("{}: delete file {}", msg, name); | ||
super.deleteFile(name); | ||
fileLengthCache.remove(name); | ||
} | ||
|
||
@Override | ||
|
@@ -638,13 +648,77 @@ public void deleteFile(String name) throws IOException { | |
} | ||
|
||
private void innerClose() throws IOException { | ||
super.close(); | ||
try { | ||
super.close(); | ||
} finally { | ||
fileLengthCache.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public IndexOutput createOutput(final String name, IOContext context) throws IOException { | ||
final IndexOutput output = super.createOutput(name, context); | ||
fileLengthCache.remove(name); // protection against multiple writes | ||
return new IndexOutput(output.toString()) { | ||
@Override | ||
public void close() throws IOException { | ||
output.close(); | ||
fileLengthCache.put(name, getFilePointer()); | ||
} | ||
|
||
@Override | ||
public long getFilePointer() { | ||
return output.getFilePointer(); | ||
} | ||
|
||
@Override | ||
public long getChecksum() throws IOException { | ||
return output.getChecksum(); | ||
} | ||
|
||
@Override | ||
public void writeByte(byte b) throws IOException { | ||
output.writeByte(b); | ||
} | ||
|
||
@Override | ||
public void writeBytes(byte[] b, int offset, int length) throws IOException { | ||
output.writeBytes(b, offset, length); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "store(" + in.toString() + ")"; | ||
} | ||
|
||
@Override | ||
public void renameFile(String source, String dest) throws IOException { | ||
final long len = fileLength(source); | ||
super.renameFile(source, dest); | ||
fileLengthCache.put(dest, len); | ||
fileLengthCache.remove(source); | ||
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. Should we call remove before put? (Was just trying to think about what would happen if source == dest) |
||
} | ||
|
||
@Override | ||
public long fileLength(String name) throws IOException { | ||
final Long size = fileLengthCache.get(name); | ||
if (size == null) { | ||
return super.fileLength(name); | ||
} else { | ||
assert size == super.fileLength(name) : size + " != " + super.fileLength(name); | ||
return size; | ||
} | ||
} | ||
|
||
long estimatedSize() { | ||
long sum = 0; | ||
for (Long len : fileLengthCache.values()) { | ||
sum += len; | ||
} | ||
return sum; | ||
} | ||
} | ||
|
||
/** Log that we are about to delete this file, to the index.store.deletes component. */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you also mention that fileLength is expensive?