forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-8451] [serializers] Make Scala tuple serializer deserializatio…
…n more failure tolerant This closes apache#5567.
- Loading branch information
Showing
6 changed files
with
231 additions
and
7 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
Binary file added
BIN
+97 Bytes
flink-scala/src/test/resources/flink-1.3.2-scala-types-serializer-data
Binary file not shown.
Binary file added
BIN
+7.46 KB
flink-scala/src/test/resources/flink-1.3.2-scala-types-serializer-snapshot
Binary file not shown.
86 changes: 86 additions & 0 deletions
86
.../src/test/scala/org/apache/flink/api/scala/runtime/TupleSerializerCompatibilityTest.scala
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,86 @@ | ||
/* | ||
* 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.flink.api.scala.runtime | ||
|
||
import java.io.InputStream | ||
|
||
import org.apache.flink.api.common.ExecutionConfig | ||
import org.apache.flink.api.common.typeutils.TypeSerializerSerializationUtil | ||
import org.apache.flink.api.java.typeutils.runtime.TupleSerializerConfigSnapshot | ||
import org.apache.flink.api.scala.createTypeInformation | ||
import org.apache.flink.api.scala.runtime.TupleSerializerCompatibilityTestGenerator._ | ||
import org.apache.flink.api.scala.typeutils.CaseClassSerializer | ||
import org.apache.flink.core.memory.DataInputViewStreamWrapper | ||
import org.junit.Assert.{assertEquals, assertFalse, assertNotNull, assertTrue} | ||
import org.junit.Test | ||
|
||
/** | ||
* Test for ensuring backwards compatibility of tuples and case classes across Scala versions. | ||
*/ | ||
class TupleSerializerCompatibilityTest { | ||
|
||
@Test | ||
def testCompatibilityWithFlink_1_3(): Unit = { | ||
var is: InputStream = null | ||
try { | ||
is = getClass.getClassLoader.getResourceAsStream(SNAPSHOT_RESOURCE) | ||
val snapshotIn = new DataInputViewStreamWrapper(is) | ||
|
||
val deserialized = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience( | ||
snapshotIn, | ||
getClass.getClassLoader) | ||
|
||
assertEquals(1, deserialized.size) | ||
|
||
val oldSerializer = deserialized.get(0).f0 | ||
val oldConfigSnapshot = deserialized.get(0).f1 | ||
|
||
// test serializer and config snapshot | ||
assertNotNull(oldSerializer) | ||
assertNotNull(oldConfigSnapshot) | ||
assertTrue(oldSerializer.isInstanceOf[CaseClassSerializer[_]]) | ||
assertTrue(oldConfigSnapshot.isInstanceOf[TupleSerializerConfigSnapshot[_]]) | ||
|
||
val currentSerializer = createTypeInformation[TestCaseClass] | ||
.createSerializer(new ExecutionConfig()) | ||
assertFalse(currentSerializer.ensureCompatibility(oldConfigSnapshot).isRequiresMigration) | ||
|
||
// test old data serialization | ||
is.close() | ||
is = getClass.getClassLoader.getResourceAsStream(DATA_RESOURCE) | ||
var dataIn = new DataInputViewStreamWrapper(is) | ||
|
||
assertEquals(TEST_DATA_1, oldSerializer.deserialize(dataIn)) | ||
assertEquals(TEST_DATA_2, oldSerializer.deserialize(dataIn)) | ||
assertEquals(TEST_DATA_3, oldSerializer.deserialize(dataIn)) | ||
|
||
// test new data serialization | ||
is.close() | ||
is = getClass.getClassLoader.getResourceAsStream(DATA_RESOURCE) | ||
dataIn = new DataInputViewStreamWrapper(is) | ||
assertEquals(TEST_DATA_1, currentSerializer.deserialize(dataIn)) | ||
assertEquals(TEST_DATA_2, currentSerializer.deserialize(dataIn)) | ||
assertEquals(TEST_DATA_3, currentSerializer.deserialize(dataIn)) | ||
} finally { | ||
if (is != null) { | ||
is.close() | ||
} | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
.../scala/org/apache/flink/api/scala/runtime/TupleSerializerCompatibilityTestGenerator.scala
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,94 @@ | ||
/* | ||
* 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.flink.api.scala.runtime | ||
|
||
import java.io.FileOutputStream | ||
import java.util.Collections | ||
|
||
import org.apache.flink.api.common.ExecutionConfig | ||
import org.apache.flink.api.common.typeutils.TypeSerializerSerializationUtil | ||
import org.apache.flink.core.memory.DataOutputViewStreamWrapper | ||
|
||
/** | ||
* Run this code on a 1.3 (or earlier) branch to generate the test data | ||
* for the [[TupleSerializerCompatibilityTest]]. | ||
* | ||
* This generator is a separate file because a companion object would have side-effects on the | ||
* annotated classes generated by Scala. | ||
*/ | ||
object TupleSerializerCompatibilityTestGenerator { | ||
|
||
case class TestCaseClass( | ||
i: Int, | ||
e: Either[String, Unit], | ||
t2: (Boolean, String), | ||
t1: (Double), | ||
t2ii: (Int, Int)) | ||
|
||
val TEST_DATA_1 = TestCaseClass(42, Left("Hello"), (false, "what?"), 12.2, (12, 12)) | ||
val TEST_DATA_2 = TestCaseClass(42, Right(), (false, "what?"), 12.2, (100, 200)) | ||
val TEST_DATA_3 = TestCaseClass(100, Left("Hello"), (true, "what?"), 14.2, (-1, Int.MinValue)) | ||
|
||
val SNAPSHOT_RESOURCE: String = "flink-1.3.2-scala-types-serializer-snapshot" | ||
|
||
val DATA_RESOURCE: String = "flink-1.3.2-scala-types-serializer-data" | ||
|
||
val SNAPSHOT_OUTPUT_PATH: String = "/tmp/snapshot/" + SNAPSHOT_RESOURCE | ||
|
||
val DATA_OUTPUT_PATH: String = "/tmp/snapshot/" + DATA_RESOURCE | ||
|
||
def main(args: Array[String]): Unit = { | ||
|
||
val typeInfo = org.apache.flink.api.scala.createTypeInformation[TestCaseClass] | ||
|
||
val serializer = typeInfo.createSerializer(new ExecutionConfig()) | ||
val configSnapshot = serializer.snapshotConfiguration() | ||
|
||
var fos: FileOutputStream = null | ||
try { | ||
fos = new FileOutputStream(SNAPSHOT_OUTPUT_PATH) | ||
val out = new DataOutputViewStreamWrapper(fos) | ||
|
||
TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience( | ||
out, | ||
Collections.singletonList( | ||
new org.apache.flink.api.java.tuple.Tuple2(serializer, configSnapshot) | ||
) | ||
) | ||
} finally { | ||
if (fos != null) { | ||
fos.close() | ||
} | ||
} | ||
|
||
fos = null | ||
try { | ||
fos = new FileOutputStream(DATA_OUTPUT_PATH) | ||
val out = new DataOutputViewStreamWrapper(fos) | ||
|
||
serializer.serialize(TEST_DATA_1, out) | ||
serializer.serialize(TEST_DATA_2, out) | ||
serializer.serialize(TEST_DATA_3, out) | ||
} finally { | ||
if (fos != null) { | ||
fos.close() | ||
} | ||
} | ||
} | ||
} |