-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-6025] [core] Add Flink's own JavaSerializer for Kryo serializa…
…tion This commit adds a reimplemented JavaSerializer to be registered with Kryo. This is due to a know issue with Kryo's JavaSerializer that may use the wrong classloader for deserialzation. Instead of registering Kryo's JavaSerializer for Throwables, it is now changed to register the reimplemented JavaSerializer. Users who bump into ClassNotFoundExceptions if they are using Kryo's JavaSerializer for their own types are also recommended to change to Flink's JavaSerializer.
- Loading branch information
Showing
4 changed files
with
96 additions
and
4 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
81 changes: 81 additions & 0 deletions
81
...k-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/kryo/JavaSerializer.java
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,81 @@ | ||
/* | ||
* 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.java.typeutils.runtime.kryo; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.KryoException; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
import com.esotericsoftware.kryo.util.ObjectMap; | ||
import org.apache.flink.util.InstantiationUtil; | ||
|
||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
/** | ||
* This is a reimplementation of Kryo's {@link com.esotericsoftware.kryo.serializers.JavaSerializer}, | ||
* that additionally makes sure the {@link ObjectInputStream} used for deserialization specifically uses Kryo's | ||
* registered classloader. | ||
* | ||
* Flink maintains this reimplementation due to a known issue with Kryo's {@code JavaSerializer}, in which the wrong | ||
* classloader may be used for deserialization, leading to {@link ClassNotFoundException}s. | ||
* | ||
* @see <a href="https://issues.apache.org/jira/browse/FLINK-6025">FLINK-6025</a> | ||
* @see <a href="https://github.com/EsotericSoftware/kryo/pull/483">Known issue with Kryo's JavaSerializer</a> | ||
* | ||
* @param <T> The type to be serialized. | ||
*/ | ||
public class JavaSerializer<T> extends Serializer<T> { | ||
|
||
public JavaSerializer() {} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void write(Kryo kryo, Output output, T o) { | ||
try { | ||
ObjectMap graphContext = kryo.getGraphContext(); | ||
ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this); | ||
if (objectStream == null) { | ||
objectStream = new ObjectOutputStream(output); | ||
graphContext.put(this, objectStream); | ||
} | ||
objectStream.writeObject(o); | ||
objectStream.flush(); | ||
} catch (Exception ex) { | ||
throw new KryoException("Error during Java serialization.", ex); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T read(Kryo kryo, Input input, Class aClass) { | ||
try { | ||
ObjectMap graphContext = kryo.getGraphContext(); | ||
ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this); | ||
if (objectStream == null) { | ||
objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader()); | ||
graphContext.put(this, objectStream); | ||
} | ||
return (T) objectStream.readObject(); | ||
} catch (Exception ex) { | ||
throw new KryoException("Error during Java deserialization.", ex); | ||
} | ||
} | ||
|
||
} |
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