-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-18188] Add checksum for shuffle blocks #15894
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,12 @@ | |
|
||
package org.apache.spark.network.buffer; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
import java.io.*; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.util.zip.Adler32; | ||
import java.util.zip.CheckedInputStream; | ||
import java.util.zip.Checksum; | ||
|
||
import com.google.common.base.Objects; | ||
import com.google.common.io.ByteStreams; | ||
|
@@ -92,12 +91,27 @@ public ByteBuffer nioByteBuffer() throws IOException { | |
} | ||
|
||
@Override | ||
public InputStream createInputStream() throws IOException { | ||
public InputStream createInputStream(boolean checksum) throws IOException { | ||
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. So, is this only for testing? because it's otherwise very expensive to compute, requiring two passes over the file. If so, should it not be in some more package-private method only and not exposed? 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. This is a good question. Actually we already have checksum along with compression, we could move the decompression a little bit earlier to detect the corruption in block fetcher, will try that soon. |
||
FileInputStream is = null; | ||
try { | ||
is = new FileInputStream(file); | ||
ByteStreams.skipFully(is, offset); | ||
return new LimitedInputStream(is, length); | ||
if (checksum) { | ||
Checksum ck = new Adler32(); | ||
DataInputStream din = new DataInputStream(new CheckedInputStream(is, ck)); | ||
ByteStreams.skipFully(din, length - 8); | ||
long sum = ck.getValue(); | ||
long expected = din.readLong(); | ||
if (sum != expected) { | ||
throw new IOException("Checksum does not match " + sum + "!=" + expected); | ||
} | ||
is.close(); | ||
is = new FileInputStream(file); | ||
ByteStreams.skipFully(is, offset); | ||
return new LimitedInputStream(is, length - 8); | ||
} else { | ||
return new LimitedInputStream(is, length); | ||
} | ||
} catch (IOException e) { | ||
try { | ||
if (is != null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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. | ||
*/ | ||
|
||
package org.apache.spark.storage; | ||
|
||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
import java.util.zip.Checksum; | ||
|
||
/** | ||
* A output stream that generate checksum for written data and write the checksum as long at | ||
* the end of stream. | ||
*/ | ||
public class ChecksumOutputStream extends FilterOutputStream { | ||
private Checksum cksum; | ||
private boolean closed; | ||
|
||
public ChecksumOutputStream(OutputStream out, Checksum cksum) { | ||
super(out); | ||
cksum.reset(); | ||
this.cksum = cksum; | ||
this.closed = false; | ||
} | ||
|
||
public void write(int b) throws IOException { | ||
out.write(b); | ||
cksum.update(b); | ||
} | ||
|
||
public void write(byte[] b) throws IOException { | ||
write(b, 0, b.length); | ||
} | ||
|
||
public void write(byte[] b, int off, int len) throws IOException { | ||
out.write(b, off, len); | ||
cksum.update(b, off, len); | ||
} | ||
|
||
public void close() throws IOException { | ||
flush(); | ||
if (!closed) { | ||
closed = true; | ||
ByteBuffer buffer = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN); | ||
buffer.putLong(cksum.getValue()); | ||
out.write(buffer.array()); | ||
out.close(); | ||
} | ||
} | ||
} |
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.
would be better to abstract out the checksum functions into some class so it's easier to change in the future.