Skip to content

Speedup reading TranslogHeader #112073

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static int headerSizeInBytes(int version, int uuidLength) {
return size;
}

static int readHeaderVersion(final Path path, final FileChannel channel, final StreamInput in) throws IOException {
static int readHeaderVersion(final Path path, final StreamInput in) throws IOException {
final int version;
try {
version = CodecUtil.checkHeader(new InputStreamDataInput(in), TRANSLOG_CODEC, VERSION_PRIMARY_TERM, VERSION_PRIMARY_TERM);
Expand All @@ -109,24 +109,25 @@ static int readHeaderVersion(final Path path, final FileChannel channel, final S
* Read a translog header from the given path and file channel
*/
static TranslogHeader read(final String translogUUID, final Path path, final FileChannel channel) throws IOException {
final long fileSize = channel.size();
try {
// This input is intentionally not closed because closing it will close the FileChannel.
final BufferedChecksumStreamInput in = new BufferedChecksumStreamInput(
new InputStreamStreamInput(java.nio.channels.Channels.newInputStream(channel), channel.size()),
new InputStreamStreamInput(java.nio.channels.Channels.newInputStream(channel), fileSize),
path.toString()
);
final int version = readHeaderVersion(path, channel, in);
final int version = readHeaderVersion(path, in);
// Read the translogUUID
final int uuidLen = in.readInt();
if (uuidLen > channel.size()) {
if (uuidLen > fileSize) {
throw new TranslogCorruptedException(path.toString(), "UUID length can't be larger than the translog");
}
if (uuidLen <= 0) {
throw new TranslogCorruptedException(path.toString(), "UUID length must be positive");
}
final BytesRef uuid = new BytesRef(uuidLen);
uuid.length = uuidLen;
in.read(uuid.bytes, uuid.offset, uuid.length);
in.readBytes(uuid.bytes, uuid.offset, uuid.length);
// Read the primary term
assert version == VERSION_PRIMARY_TERM;
final long primaryTerm = in.readLong();
Expand Down