|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Licensed to Elasticsearch under one or more contributor |
| 11 | + * license agreements. See the NOTICE file distributed with |
| 12 | + * this work for additional information regarding copyright |
| 13 | + * ownership. Elasticsearch licenses this file to you under |
| 14 | + * the Apache License, Version 2.0 (the "License"); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, |
| 21 | + * software distributed under the License is distributed on an |
| 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | + * KIND, either express or implied. See the License for the |
| 24 | + * specific language governing permissions and limitations |
| 25 | + * under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | + * Modifications Copyright OpenSearch Contributors. See |
| 30 | + * GitHub history for details. |
| 31 | + */ |
| 32 | + |
| 33 | +package org.opensearch.index.translog; |
| 34 | + |
| 35 | +import org.apache.lucene.codecs.CodecUtil; |
| 36 | +import org.apache.lucene.store.OutputStreamDataOutput; |
| 37 | +import org.opensearch.common.io.Channels; |
| 38 | +import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; |
| 39 | + |
| 40 | +import java.io.ByteArrayOutputStream; |
| 41 | +import java.io.IOException; |
| 42 | +import java.nio.ByteBuffer; |
| 43 | +import java.nio.channels.FileChannel; |
| 44 | +import java.nio.file.Path; |
| 45 | +import java.nio.file.StandardOpenOption; |
| 46 | + |
| 47 | +/** |
| 48 | + * Each translog file is started with a header followed by the translog operations, and ending with a footer. |
| 49 | + * The footer encapsulates the checksum of the translog. |
| 50 | + * */ |
| 51 | +public class TranslogFooter { |
| 52 | + |
| 53 | + /** |
| 54 | + * footerLength returns the length of the footer. |
| 55 | + * We are writing 16 bytes and therefore, we return the same. |
| 56 | + */ |
| 57 | + static int footerLength() { |
| 58 | + return 16; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * write the translog footer which records both checksum and algorithm ID. |
| 63 | + * This method is based upon the CodecUtils.writeFooter method. |
| 64 | + * This footer can be parsed and read with TranslogFooter.readChecksum(). |
| 65 | + * |
| 66 | + * Similar to CodecUtils documentation, the footer consists of- |
| 67 | + * Footer --> Magic,AlgorithmID,Checksum |
| 68 | + * Magic --> Uint32. This identifies the start of the footer. It is always -1071082520. |
| 69 | + * AlgorithmID --> Uint32. This indicates the checksum algorithm used. Currently, this is always 0. |
| 70 | + * Checksum --> Uint64. This is the checksum as calculated for the translog. |
| 71 | + * */ |
| 72 | + static byte[] write(FileChannel channel, long checksum, boolean toSync) throws IOException { |
| 73 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 74 | + final OutputStreamDataOutput out = new OutputStreamDataOutput(new OutputStreamStreamOutput(byteArrayOutputStream)); |
| 75 | + |
| 76 | + CodecUtil.writeBEInt(out, CodecUtil.FOOTER_MAGIC); |
| 77 | + CodecUtil.writeBEInt(out, 0); |
| 78 | + CodecUtil.writeBELong(out, checksum); |
| 79 | + |
| 80 | + Channels.writeToChannel(byteArrayOutputStream.toByteArray(), channel); |
| 81 | + if (toSync) { |
| 82 | + channel.force(false); |
| 83 | + } |
| 84 | + |
| 85 | + return byteArrayOutputStream.toByteArray(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * readChecksum reads the translog file from the given location and returns the checksum if present in the footer. |
| 90 | + * If the translog file is of older version and the footer is not present, then we return null. |
| 91 | + * */ |
| 92 | + static Long readChecksum(Path path) throws IOException { |
| 93 | + try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) { |
| 94 | + // Read the header and find out if the footer is supported. |
| 95 | + final TranslogHeader header = TranslogHeader.read(path, channel); |
| 96 | + if (header.getTranslogHeaderVersion() < TranslogHeader.VERSION_WITH_FOOTER) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + // Read the footer. |
| 101 | + final long fileSize = channel.size(); |
| 102 | + final long footerStart = fileSize - TranslogFooter.footerLength(); |
| 103 | + ByteBuffer footer = ByteBuffer.allocate(TranslogFooter.footerLength()); |
| 104 | + int bytesRead = Channels.readFromFileChannel(channel, footerStart, footer); |
| 105 | + if (bytesRead != TranslogFooter.footerLength()) { |
| 106 | + throw new IOException( |
| 107 | + "Read " + bytesRead + " bytes from footer instead of expected " + TranslogFooter.footerLength() + " bytes" |
| 108 | + ); |
| 109 | + } |
| 110 | + footer.flip(); |
| 111 | + |
| 112 | + // Validate the footer and return the checksum. |
| 113 | + int magic = footer.getInt(); |
| 114 | + if (magic != CodecUtil.FOOTER_MAGIC) { |
| 115 | + throw new IOException("Invalid footer magic number: " + magic); |
| 116 | + } |
| 117 | + |
| 118 | + int algorithmId = footer.getInt(); |
| 119 | + if (algorithmId != 0) { |
| 120 | + throw new IOException("Unsupported checksum algorithm ID: " + algorithmId); |
| 121 | + } |
| 122 | + |
| 123 | + return footer.getLong(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments