-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for incremental download of translog files
Presently, the download workflow for remote backed storage works in a manner which causes the download for same translog files multiple times, each time deleting all the older files before downloading them again. This causes significant wasted network bandwidth, along with the time taken for the shard to become active. This change adds support for downloading the translog files incrementally and omitting the same if they are present locally. Signed-off-by: Harsh Rawat <[email protected]>
- Loading branch information
Harsh Rawat
committed
Oct 7, 2024
1 parent
421a1cc
commit cd08d89
Showing
11 changed files
with
337 additions
and
34 deletions.
There are no files selected for viewing
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
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
135 changes: 135 additions & 0 deletions
135
server/src/main/java/org/opensearch/index/translog/TranslogFooter.java
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.index.translog; | ||
|
||
import org.apache.lucene.codecs.CodecUtil; | ||
import org.apache.lucene.store.OutputStreamDataOutput; | ||
import org.opensearch.common.io.Channels; | ||
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
/** | ||
* Each translog file is started with a header followed by the translog operations, and ending with a footer. | ||
* The footer encapsulates the checksum of the translog. | ||
* */ | ||
public class TranslogFooter { | ||
|
||
/** | ||
* footerLength returns the length of the footer. | ||
* We are writing 16 bytes and therefore, we return the same. | ||
*/ | ||
static int footerLength() { | ||
return 16; | ||
} | ||
|
||
/** | ||
* write write the translog footer which records both checksum and algorithm ID. | ||
* This method is based upon the CodecUtils.writeFooter method. | ||
* This footer can be parsed and read with CodecUtils.readFooter(). | ||
* | ||
* Same as CodecUtils documentation- | ||
* Footer --> Magic,AlgorithmID,Checksum | ||
* Magic --> Uint32. This identifies the start of the footer. It is always -1071082520. | ||
* AlgorithmID --> Uint32. This indicates the checksum algorithm used. Currently this is always 0. | ||
* Checksum --> Uint64. This is the checksum as calculated for the translog. | ||
* */ | ||
static void write(FileChannel channel, long checksum, boolean toSync) throws IOException { | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
final OutputStreamDataOutput out = new OutputStreamDataOutput(new OutputStreamStreamOutput(byteArrayOutputStream)); | ||
|
||
CodecUtil.writeBEInt(out, CodecUtil.FOOTER_MAGIC); | ||
CodecUtil.writeBEInt(out, 0); | ||
CodecUtil.writeBELong(out, checksum); | ||
|
||
Channels.writeToChannel(byteArrayOutputStream.toByteArray(), channel); | ||
if (toSync) { | ||
channel.force(false); | ||
} | ||
} | ||
|
||
/** | ||
* validate checks if the Translog file has a valid footer. | ||
* */ | ||
static boolean validate(Path path) { | ||
try { | ||
return TranslogFooter.readChecksum(path) != null; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* readChecksum reads the translog file from the given location and returns the checksum if present in the footer. | ||
* If the translog file is of older version and the footer is not present, then we return null. | ||
* */ | ||
static Long readChecksum(Path path) throws IOException { | ||
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) { | ||
// Read the header and find out if the footer is supported. | ||
final TranslogHeader header = TranslogHeader.read(path, channel); | ||
if (header.getTranslogHeaderVersion() < TranslogHeader.VERSION_WITH_FOOTER) { | ||
return null; | ||
} | ||
|
||
// Read the footer. | ||
final long fileSize = channel.size(); | ||
final long footerStart = fileSize - TranslogFooter.footerLength(); | ||
ByteBuffer footer = ByteBuffer.allocate(TranslogFooter.footerLength()); | ||
int bytesRead = Channels.readFromFileChannel(channel, footerStart, footer); | ||
if (bytesRead != TranslogFooter.footerLength()) { | ||
throw new IOException( | ||
"Read " + bytesRead + " bytes from footer instead of expected " + TranslogFooter.footerLength() + " bytes" | ||
); | ||
} | ||
footer.flip(); | ||
|
||
// Validate the footer and return the checksum. | ||
int magic = footer.getInt(); | ||
if (magic != CodecUtil.FOOTER_MAGIC) { | ||
throw new IOException("Invalid footer magic number: " + magic); | ||
} | ||
|
||
int algorithmId = footer.getInt(); | ||
if (algorithmId != 0) { | ||
throw new IOException("Unsupported checksum algorithm ID: " + algorithmId); | ||
} | ||
|
||
return footer.getLong(); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.