Skip to content
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

adding SparkTestUtils.roundTripThroughJavaSerialization #5257

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
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 @@ -6,6 +6,7 @@
import htsjdk.variant.vcf.VCFCodec;
import org.broadinstitute.barclay.argparser.CommandLineException;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.testutils.SparkTestUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -210,22 +211,9 @@ public void testFeatureCodecCache() {
@SuppressWarnings("unchecked")
@Test
public void testFeatureCodecCacheSerialization() throws IOException, ClassNotFoundException {
FeatureInput<VariantContext> featureInput = getVariantFeatureInputWithCachedCodec();

// serialize
byte[] serializedFeatureInput;
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(featureInput);
serializedFeatureInput = bos.toByteArray();
}
Assert.assertNotNull(serializedFeatureInput);

// deserialize
FeatureInput<VariantContext> roundTrippedFeatureInput;
try (final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedFeatureInput))) {
roundTrippedFeatureInput = (FeatureInput<VariantContext>) ois.readObject();
}
final FeatureInput<VariantContext>featureInput = getVariantFeatureInputWithCachedCodec();

final FeatureInput<VariantContext> roundTrippedFeatureInput = SparkTestUtils.roundTripThroughJavaSerialization(featureInput);
Assert.assertNotNull(roundTrippedFeatureInput);

// we expect to lose the cached feature codec class on serialization, but retain the feature path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.testutils.SparkTestUtils;
import org.testng.annotations.Test;

import java.io.*;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.engine.spark.datasources.ReferenceFileSparkSource;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.testng.annotations.Test;

public class ReferenceFileSparkSourceUnitTest extends GATKBaseTest {

Expand Down Expand Up @@ -42,13 +43,9 @@ public void testDatasourcesReferenceSerializes() throws IOException, ClassNotFou

final ReferenceFileSparkSource referenceFileSource = new ReferenceFileSparkSource(refPath);

// Can we serialize it?
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(referenceFileSource);
//can we serialize it?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we can!

ReferenceFileSparkSource otherSide = SparkTestUtils.roundTripThroughJavaSerialization(referenceFileSource);

ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
ReferenceFileSparkSource otherSide = (ReferenceFileSparkSource)is.readObject();
// After deserialization, will it crash?
otherSide.getReferenceSequenceDictionary(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import scala.reflect.ClassTag;
import scala.reflect.ClassTag$;

import java.io.*;

public final class SparkTestUtils {
private SparkTestUtils() {}

Expand All @@ -29,4 +31,28 @@ public static <T> T roundTripInKryo(final T input, final Class<?> inputClazz, fi
final ClassTag<T> tag = ClassTag$.MODULE$.apply(inputClazz);
return sparkSerializer.deserialize(sparkSerializer.serialize(input, tag), tag);
}

/**
* Takes an input object and returns the value of the object after it has been serialized and then deserialized
* using Java's built in serialization.
*
* @param input an object to be serialized. Never {@code null}
* @return serialized and deserialized instance of input, may throw if serialization fails
*/
@SuppressWarnings("unchecked")
public static <T> T roundTripThroughJavaSerialization(T input) throws IOException, ClassNotFoundException {
Utils.nonNull(input);
final byte[] serializedBytes;
try(final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(baos)){
out.writeObject(input);
serializedBytes = baos.toByteArray();
}

try (final ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes);
final ObjectInputStream in = new ObjectInputStream(bais)){

return (T) in.readObject();
}
}
}