|
11 | 11 | import org.apache.lucene.index.CorruptIndexException;
|
12 | 12 | import org.apache.lucene.index.IndexFormatTooNewException;
|
13 | 13 | import org.apache.lucene.index.IndexFormatTooOldException;
|
14 |
| -import org.apache.lucene.store.ByteBuffersDataInput; |
15 |
| -import org.apache.lucene.store.ByteBuffersIndexInput; |
16 |
| -import org.apache.lucene.store.IndexInput; |
| 14 | +import org.apache.lucene.store.ChecksumIndexInput; |
| 15 | +import org.apache.lucene.store.InputStreamDataInput; |
17 | 16 | import org.apache.lucene.store.OutputStreamIndexOutput;
|
18 |
| -import org.apache.lucene.util.BytesRef; |
19 | 17 | import org.elasticsearch.cluster.metadata.Metadata;
|
20 | 18 | import org.elasticsearch.common.CheckedConsumer;
|
21 | 19 | import org.elasticsearch.common.CheckedFunction;
|
| 20 | +import org.elasticsearch.common.Numbers; |
22 | 21 | import org.elasticsearch.common.blobstore.BlobContainer;
|
| 22 | +import org.elasticsearch.common.bytes.BytesArray; |
23 | 23 | import org.elasticsearch.common.bytes.BytesReference;
|
24 | 24 | import org.elasticsearch.common.compress.CompressorFactory;
|
| 25 | +import org.elasticsearch.common.io.Streams; |
25 | 26 | import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput;
|
26 |
| -import org.elasticsearch.common.lucene.store.ByteArrayIndexInput; |
27 | 27 | import org.elasticsearch.common.lucene.store.IndexOutputOutputStream;
|
28 | 28 | import org.elasticsearch.common.util.BigArrays;
|
29 | 29 | import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
30 | 30 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
31 | 31 | import org.elasticsearch.common.xcontent.ToXContent;
|
32 | 32 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
33 | 33 | import org.elasticsearch.common.xcontent.XContentFactory;
|
34 |
| -import org.elasticsearch.common.xcontent.XContentHelper; |
35 | 34 | import org.elasticsearch.common.xcontent.XContentParser;
|
36 | 35 | import org.elasticsearch.common.xcontent.XContentType;
|
37 |
| -import org.elasticsearch.core.internal.io.Streams; |
38 | 36 | import org.elasticsearch.gateway.CorruptStateException;
|
39 | 37 | import org.elasticsearch.snapshots.SnapshotInfo;
|
40 | 38 |
|
| 39 | +import java.io.FilterInputStream; |
41 | 40 | import java.io.IOException;
|
42 | 41 | import java.io.InputStream;
|
43 | 42 | import java.io.OutputStream;
|
44 |
| -import java.util.Arrays; |
45 | 43 | import java.util.HashMap;
|
46 | 44 | import java.util.Locale;
|
47 | 45 | import java.util.Map;
|
| 46 | +import java.util.zip.CRC32; |
48 | 47 |
|
49 | 48 | /**
|
50 | 49 | * Snapshot metadata file format used in v2.0 and above
|
@@ -93,37 +92,174 @@ public ChecksumBlobStoreFormat(String codec, String blobNameFormat, CheckedFunct
|
93 | 92 | * @param name name to be translated into
|
94 | 93 | * @return parsed blob object
|
95 | 94 | */
|
96 |
| - public T read(BlobContainer blobContainer, String name, NamedXContentRegistry namedXContentRegistry, |
97 |
| - BigArrays bigArrays) throws IOException { |
| 95 | + public T read(BlobContainer blobContainer, String name, NamedXContentRegistry namedXContentRegistry) throws IOException { |
98 | 96 | String blobName = blobName(name);
|
99 |
| - try (ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays); |
100 |
| - InputStream in = blobContainer.readBlob(blobName)) { |
101 |
| - Streams.copy(in, out, false); |
102 |
| - return deserialize(blobName, namedXContentRegistry, out.bytes()); |
| 97 | + try (InputStream in = blobContainer.readBlob(blobName)) { |
| 98 | + return deserialize(namedXContentRegistry, in); |
103 | 99 | }
|
104 | 100 | }
|
105 | 101 |
|
106 | 102 | public String blobName(String name) {
|
107 | 103 | return String.format(Locale.ROOT, blobNameFormat, name);
|
108 | 104 | }
|
109 | 105 |
|
110 |
| - public T deserialize(String blobName, NamedXContentRegistry namedXContentRegistry, BytesReference bytes) throws IOException { |
111 |
| - final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")"; |
| 106 | + public T deserialize(NamedXContentRegistry namedXContentRegistry, InputStream input) throws IOException { |
| 107 | + final DeserializeMetaBlobInputStream deserializeMetaBlobInputStream = new DeserializeMetaBlobInputStream(input); |
112 | 108 | try {
|
113 |
| - final IndexInput indexInput = bytes.length() > 0 ? new ByteBuffersIndexInput( |
114 |
| - new ByteBuffersDataInput(Arrays.asList(BytesReference.toByteBuffers(bytes))), resourceDesc) |
115 |
| - : new ByteArrayIndexInput(resourceDesc, BytesRef.EMPTY_BYTES); |
116 |
| - CodecUtil.checksumEntireFile(indexInput); |
117 |
| - CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION); |
118 |
| - long filePointer = indexInput.getFilePointer(); |
119 |
| - long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer; |
120 |
| - try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, |
121 |
| - bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) { |
122 |
| - return reader.apply(parser); |
| 109 | + CodecUtil.checkHeader(new InputStreamDataInput(deserializeMetaBlobInputStream), codec, VERSION, VERSION); |
| 110 | + final InputStream wrappedStream; |
| 111 | + if (deserializeMetaBlobInputStream.nextBytesCompressed()) { |
| 112 | + wrappedStream = CompressorFactory.COMPRESSOR.threadLocalInputStream(deserializeMetaBlobInputStream); |
| 113 | + } else { |
| 114 | + wrappedStream = deserializeMetaBlobInputStream; |
123 | 115 | }
|
| 116 | + final T result; |
| 117 | + try (XContentParser parser = XContentType.SMILE.xContent().createParser( |
| 118 | + namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, wrappedStream)) { |
| 119 | + result = reader.apply(parser); |
| 120 | + } |
| 121 | + deserializeMetaBlobInputStream.verifyFooter(); |
| 122 | + return result; |
124 | 123 | } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
|
125 | 124 | // we trick this into a dedicated exception with the original stacktrace
|
126 | 125 | throw new CorruptStateException(ex);
|
| 126 | + } catch (Exception e) { |
| 127 | + try { |
| 128 | + // drain stream fully and check whether the footer is corrupted |
| 129 | + Streams.consumeFully(deserializeMetaBlobInputStream); |
| 130 | + deserializeMetaBlobInputStream.verifyFooter(); |
| 131 | + } catch (CorruptStateException cse) { |
| 132 | + cse.addSuppressed(e); |
| 133 | + throw cse; |
| 134 | + } catch (Exception ex) { |
| 135 | + e.addSuppressed(ex); |
| 136 | + } |
| 137 | + throw e; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Wrapper input stream for deserializing blobs that come with a Lucene header and footer in a streaming manner. It manually manages |
| 143 | + * a read buffer to enable not reading into the last 16 bytes (the footer length) of the buffer via the standard read methods so that |
| 144 | + * a parser backed by this stream will only see the blob's body. |
| 145 | + */ |
| 146 | + private static final class DeserializeMetaBlobInputStream extends FilterInputStream { |
| 147 | + |
| 148 | + // checksum updated with all but the last 8 bytes read from the wrapped stream |
| 149 | + private final CRC32 crc32 = new CRC32(); |
| 150 | + |
| 151 | + // Only the first buffer.length - 16 bytes are exposed by the read() methods; once the read position reaches 16 bytes from the end |
| 152 | + // of the buffer the remaining 16 bytes are moved to the start of the buffer and the rest of the buffer is filled from the stream. |
| 153 | + private final byte[] buffer = new byte[1024 * 8]; |
| 154 | + |
| 155 | + // the number of bytes in the buffer, in [0, buffer.length], equal to buffer.length unless the last fill hit EOF |
| 156 | + private int bufferCount; |
| 157 | + |
| 158 | + // the current read position within the buffer, in [0, bufferCount - 16] |
| 159 | + private int bufferPos; |
| 160 | + |
| 161 | + DeserializeMetaBlobInputStream(InputStream in) { |
| 162 | + super(in); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public int read() throws IOException { |
| 167 | + if (getAvailable() <= 0) { |
| 168 | + return -1; |
| 169 | + } |
| 170 | + return buffer[bufferPos++]; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public int read(byte[] b, int off, int len) throws IOException { |
| 175 | + int remaining = len; |
| 176 | + int read = 0; |
| 177 | + while (remaining > 0) { |
| 178 | + final int r = doRead(b, off + read, remaining); |
| 179 | + if (r <= 0) { |
| 180 | + break; |
| 181 | + } |
| 182 | + read += r; |
| 183 | + remaining -= r; |
| 184 | + } |
| 185 | + if (len > 0 && remaining == len) { |
| 186 | + // nothing to read, EOF |
| 187 | + return -1; |
| 188 | + } |
| 189 | + return read; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public void close() throws IOException { |
| 194 | + // not closing the wrapped stream |
| 195 | + } |
| 196 | + |
| 197 | + private int doRead(byte[] b, int off, int len) throws IOException { |
| 198 | + final int available = getAvailable(); |
| 199 | + if (available < 0) { |
| 200 | + return -1; |
| 201 | + } |
| 202 | + final int read = Math.min(available, len); |
| 203 | + System.arraycopy(buffer, bufferPos, b, off, read); |
| 204 | + bufferPos += read; |
| 205 | + return read; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Verify footer of the bytes read by this stream the same way {@link CodecUtil#checkFooter(ChecksumIndexInput)} would. |
| 210 | + * |
| 211 | + * @throws CorruptStateException if footer is found to be corrupted |
| 212 | + */ |
| 213 | + void verifyFooter() throws CorruptStateException { |
| 214 | + if (bufferCount - bufferPos != CodecUtil.footerLength()) { |
| 215 | + throw new CorruptStateException( |
| 216 | + "should have consumed all but 16 bytes from the buffer but saw buffer pos [" + bufferPos + "] and count [" |
| 217 | + + bufferCount + "]"); |
| 218 | + } |
| 219 | + crc32.update(buffer, 0, bufferPos + 8); |
| 220 | + final int magicFound = Numbers.bytesToInt(buffer, bufferPos); |
| 221 | + if (magicFound != CodecUtil.FOOTER_MAGIC) { |
| 222 | + throw new CorruptStateException("unexpected footer magic [" + magicFound + "]"); |
| 223 | + } |
| 224 | + final int algorithmFound = Numbers.bytesToInt(buffer, bufferPos + 4); |
| 225 | + if (algorithmFound != 0) { |
| 226 | + throw new CorruptStateException("unexpected algorithm [" + algorithmFound + "]"); |
| 227 | + } |
| 228 | + final long checksum = crc32.getValue(); |
| 229 | + final long checksumInFooter = Numbers.bytesToLong(buffer, bufferPos + 8); |
| 230 | + if (checksum != checksumInFooter) { |
| 231 | + throw new CorruptStateException("checksums do not match read [" + checksum + "] but expected [" + checksumInFooter + "]"); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * @return true if the next bytes in this stream are compressed |
| 237 | + */ |
| 238 | + boolean nextBytesCompressed() { |
| 239 | + // we already have bytes buffered here because we verify the blob's header (far less than the 8k buffer size) before calling |
| 240 | + // this method |
| 241 | + assert bufferPos > 0 : "buffer position must be greater than 0 but was [" + bufferPos + "]"; |
| 242 | + return CompressorFactory.COMPRESSOR.isCompressed(new BytesArray(buffer, bufferPos, bufferCount - bufferPos)); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * @return the number of bytes available in the buffer, possibly refilling the buffer if needed |
| 247 | + */ |
| 248 | + private int getAvailable() throws IOException { |
| 249 | + final int footerLen = CodecUtil.footerLength(); |
| 250 | + if (bufferCount == 0) { |
| 251 | + // first read, fill the buffer |
| 252 | + bufferCount = Streams.readFully(in, buffer, 0, buffer.length); |
| 253 | + } else if (bufferPos == bufferCount - footerLen) { |
| 254 | + // crc and discard all but the last 16 bytes in the buffer that might be the footer bytes |
| 255 | + assert bufferCount >= footerLen; |
| 256 | + crc32.update(buffer, 0, bufferPos); |
| 257 | + System.arraycopy(buffer, bufferPos, buffer, 0, footerLen); |
| 258 | + bufferCount = footerLen + Streams.readFully(in, buffer, footerLen, buffer.length - footerLen); |
| 259 | + bufferPos = 0; |
| 260 | + } |
| 261 | + // bytes in the buffer minus 16 bytes that could be the footer |
| 262 | + return bufferCount - bufferPos - footerLen; |
127 | 263 | }
|
128 | 264 | }
|
129 | 265 |
|
|
0 commit comments