org.apache.flink.api.java.typeutils.runtime.KryoRegistration Java Examples

The following examples show how to use org.apache.flink.api.java.typeutils.runtime.KryoRegistration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static KryoRegistration tryReadWithSerializerClass(
	DataInputView in,
	ClassLoader userCodeClassLoader,
	String registeredClassname,
	Class<?> registeredClass) throws IOException {
	String serializerClassname = in.readUTF();
	Class serializerClass;
	try {
		serializerClass = Class.forName(serializerClassname, true, userCodeClassLoader);
		return new KryoRegistration(registeredClass, serializerClass);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered Kryo serializer class for class " + registeredClassname +
			" in classpath; using a dummy Kryo serializer that should be replaced as soon as" +
			" a new Kryo serializer for the class is present", e);
	}
	return null;
}
 
Example #2
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #3
Source File: KryoSerializerSnapshot.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #4
Source File: KryoSerializerSnapshotData.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static KryoRegistration tryReadWithSerializerClass(
	DataInputView in,
	ClassLoader userCodeClassLoader,
	String registeredClassname,
	Class<?> registeredClass) throws IOException {
	String serializerClassname = in.readUTF();
	Class serializerClass;
	try {
		serializerClass = Class.forName(serializerClassname, true, userCodeClassLoader);
		return new KryoRegistration(registeredClass, serializerClass);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered Kryo serializer class for class " + registeredClassname +
			" in classpath; using a dummy Kryo serializer that should be replaced as soon as" +
			" a new Kryo serializer for the class is present", e);
	}
	return null;
}
 
Example #5
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #6
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static KryoRegistration tryReadWithSerializerClass(
	DataInputView in,
	ClassLoader userCodeClassLoader,
	String registeredClassname,
	Class<?> registeredClass) throws IOException {
	String serializerClassname = in.readUTF();
	Class serializerClass;
	try {
		serializerClass = Class.forName(serializerClassname, true, userCodeClassLoader);
		return new KryoRegistration(registeredClass, serializerClass);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered Kryo serializer class for class " + registeredClassname +
			" in classpath; using a dummy Kryo serializer that should be replaced as soon as" +
			" a new Kryo serializer for the class is present", e);
	}
	return null;
}
 
Example #7
Source File: AvroKryoSerializerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void addAvroGenericDataArrayRegistration(LinkedHashMap<String, KryoRegistration> kryoRegistrations) {
	kryoRegistrations.put(
		GenericData.Array.class.getName(),
		new KryoRegistration(
			GenericData.Array.class,
			new ExecutionConfig.SerializableSerializer<>(new Serializers.SpecificInstanceCollectionSerializerForArrayList())));
}
 
Example #8
Source File: KryoSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that takes lists of registered types and their serializers, and resolve
 * them into a single list such that the result will resemble the final registration
 * result in Kryo.
 */
private static LinkedHashMap<String, KryoRegistration> buildKryoRegistrations(
		Class<?> serializedType,
		LinkedHashSet<Class<?>> registeredTypes,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithSerializerClasses,
		LinkedHashMap<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypesWithSerializers) {

	final LinkedHashMap<String, KryoRegistration> kryoRegistrations = new LinkedHashMap<>();

	kryoRegistrations.put(serializedType.getName(), new KryoRegistration(serializedType));

	for (Class<?> registeredType : checkNotNull(registeredTypes)) {
		kryoRegistrations.put(registeredType.getName(), new KryoRegistration(registeredType));
	}

	for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> registeredTypeWithSerializerClassEntry :
			checkNotNull(registeredTypesWithSerializerClasses).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerClassEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerClassEntry.getKey(),
						registeredTypeWithSerializerClassEntry.getValue()));
	}

	for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypeWithSerializerEntry :
			checkNotNull(registeredTypesWithSerializers).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerEntry.getKey(),
						registeredTypeWithSerializerEntry.getValue()));
	}

	// add Avro support if flink-avro is available; a dummy otherwise
	AvroUtils.getAvroUtils().addAvroGenericDataArrayRegistration(kryoRegistrations);

	return kryoRegistrations;
}
 
Example #9
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static <T> KryoSerializerSnapshotData<T> createFrom(
	Class<T> typeClass,
	LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
	LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
	LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	return new KryoSerializerSnapshotData<>(
		typeClass,
		optionalMapOf(defaultKryoSerializers, Class::getName),
		optionalMapOf(defaultKryoSerializerClasses, Class::getName),
		optionalMapOf(kryoRegistrations, Function.identity()));
}
 
Example #10
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static <T> KryoSerializerSnapshotData<T> createFrom(DataInputView in, ClassLoader cl) throws IOException {
	Class<T> typeClass = readTypeClass(in, cl);
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations = readKryoRegistrations(in, cl);
	LinkedOptionalMap<Class<?>, SerializableSerializer<?>> defaultSerializer = readDefaultKryoSerializers(in, cl);
	LinkedOptionalMap<Class<?>, Class<? extends Serializer<?>>> defaultSerializerClasses =
		readDefaultKryoSerializerClasses(in, cl);

	return new KryoSerializerSnapshotData<>(
		typeClass,
		defaultSerializer,
		defaultSerializerClasses,
		kryoRegistrations);
}
 
Example #11
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private KryoSerializerSnapshotData(
	Class<T> typeClass,
	LinkedOptionalMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
	LinkedOptionalMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations) {

	this.typeClass = typeClass;
	this.defaultKryoSerializers = defaultKryoSerializers;
	this.defaultKryoSerializerClasses = defaultKryoSerializerClasses;
	this.kryoRegistrations = kryoRegistrations;
}
 
Example #12
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void writeKryoRegistrations(
	DataOutputView out,
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations) throws IOException {

	writeOptionalMap(
		out,
		kryoRegistrations,
		DataOutput::writeUTF,
		KryoRegistrationUtil::writeKryoRegistration);
}
 
Example #13
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LinkedOptionalMap<String, KryoRegistration> readKryoRegistrations(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	return readOptionalMap(
		in,
		(stream, unused) -> stream.readUTF(),
		(stream, unused) -> KryoRegistrationUtil.tryReadKryoRegistration(stream, userCodeClassLoader)
	);
}
 
Example #14
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static void writeKryoRegistration(
	DataOutputView out, KryoRegistration kryoRegistration) throws IOException {

	checkNotNull(kryoRegistration);
	out.writeUTF(kryoRegistration.getRegisteredClass().getName());

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		kryoRegistration.getSerializerDefinitionType();

	out.writeInt(serializerDefinitionType.ordinal());
	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			// nothing else to write
			break;
		}
		case CLASS: {
			Class<? extends Serializer<?>> serializerClass = kryoRegistration.getSerializerClass();
			assert serializerClass != null;
			out.writeUTF(serializerClass.getName());
			break;
		}
		case INSTANCE: {
			try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) {
				InstantiationUtil.serializeObject(outViewWrapper, kryoRegistration.getSerializableSerializerInstance());
			}
			break;
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example #15
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static KryoRegistration tryReadKryoRegistration(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	String registeredClassname = in.readUTF();
	Class<?> registeredClass;
	try {
		registeredClass = Class.forName(registeredClassname, true, userCodeClassLoader);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered class " + registeredClassname + " for Kryo serialization in classpath;" +
			" using a dummy class as a placeholder.", e);
		return null;
	}

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		KryoRegistration.SerializerDefinitionType.values()[in.readInt()];

	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			return new KryoRegistration(registeredClass);
		}
		case CLASS: {
			return tryReadWithSerializerClass(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		case INSTANCE: {
			return tryReadWithSerializerInstance(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example #16
Source File: AvroKryoSerializerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void addAvroGenericDataArrayRegistration(LinkedHashMap<String, KryoRegistration> kryoRegistrations) {
	kryoRegistrations.put(
		GenericData.Array.class.getName(),
		new KryoRegistration(
			GenericData.Array.class,
			new ExecutionConfig.SerializableSerializer<>(new Serializers.SpecificInstanceCollectionSerializerForArrayList())));
}
 
Example #17
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 5 votes vote down vote up
KryoSerializerSnapshot(Class<T> typeClass,
		LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
		LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	this.snapshotData = createFrom(typeClass, defaultKryoSerializers, defaultKryoSerializerClasses, kryoRegistrations);
}
 
Example #18
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 5 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(KryoSerializer<T> newSerializer) {
	// merge the default serializers
	final MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers = mergeRightIntoLeft(
		snapshotData.getDefaultKryoSerializers(),
		optionalMapOf(newSerializer.getDefaultKryoSerializers(), Class::getName));

	if (reconfiguredDefaultKryoSerializers.hasMissingKeys()) {
		logMissingKeys(reconfiguredDefaultKryoSerializers);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// merge default serializer classes
	final MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses = mergeRightIntoLeft(
		snapshotData.getDefaultKryoSerializerClasses(),
		optionalMapOf(newSerializer.getDefaultKryoSerializerClasses(), Class::getName));

	if (reconfiguredDefaultKryoSerializerClasses.hasMissingKeys()) {
		logMissingKeys(reconfiguredDefaultKryoSerializerClasses);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// merge registration
	final MergeResult<String, KryoRegistration> reconfiguredRegistrations = mergeRightIntoLeft(
		snapshotData.getKryoRegistrations(),
		optionalMapOf(newSerializer.getKryoRegistrations(), Function.identity()));

	if (reconfiguredRegistrations.hasMissingKeys()) {
		logMissingKeys(reconfiguredRegistrations);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// there are no missing keys, now we have to decide whether we are compatible as-is or we require reconfiguration.
	return resolveSchemaCompatibility(
		reconfiguredDefaultKryoSerializers,
		reconfiguredDefaultKryoSerializerClasses,
		reconfiguredRegistrations);
}
 
Example #19
Source File: KryoSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
KryoSerializer(Class<T> type,
			   LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultSerializers,
			   LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultSerializerClasses,
			   LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	this.type = checkNotNull(type, "Type class cannot be null.");
	this.defaultSerializerClasses = checkNotNull(defaultSerializerClasses, "Default serializer classes cannot be null.");
	this.defaultSerializers = checkNotNull(defaultSerializers, "Default serializers cannot be null.");
	this.kryoRegistrations = checkNotNull(kryoRegistrations, "Kryo registrations cannot be null.");
}
 
Example #20
Source File: KryoSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that takes lists of registered types and their serializers, and resolve
 * them into a single list such that the result will resemble the final registration
 * result in Kryo.
 */
private static LinkedHashMap<String, KryoRegistration> buildKryoRegistrations(
		Class<?> serializedType,
		LinkedHashSet<Class<?>> registeredTypes,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithSerializerClasses,
		LinkedHashMap<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypesWithSerializers) {

	final LinkedHashMap<String, KryoRegistration> kryoRegistrations = new LinkedHashMap<>();

	kryoRegistrations.put(serializedType.getName(), new KryoRegistration(serializedType));

	for (Class<?> registeredType : checkNotNull(registeredTypes)) {
		kryoRegistrations.put(registeredType.getName(), new KryoRegistration(registeredType));
	}

	for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> registeredTypeWithSerializerClassEntry :
			checkNotNull(registeredTypesWithSerializerClasses).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerClassEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerClassEntry.getKey(),
						registeredTypeWithSerializerClassEntry.getValue()));
	}

	for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypeWithSerializerEntry :
			checkNotNull(registeredTypesWithSerializers).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerEntry.getKey(),
						registeredTypeWithSerializerEntry.getValue()));
	}

	// add Avro support if flink-avro is available; a dummy otherwise
	AvroUtils.getAvroUtils().addAvroGenericDataArrayRegistration(kryoRegistrations);

	return kryoRegistrations;
}
 
Example #21
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static <T> KryoSerializerSnapshotData<T> createFrom(
	Class<T> typeClass,
	LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
	LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
	LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	return new KryoSerializerSnapshotData<>(
		typeClass,
		optionalMapOf(defaultKryoSerializers, Class::getName),
		optionalMapOf(defaultKryoSerializerClasses, Class::getName),
		optionalMapOf(kryoRegistrations, Function.identity()));
}
 
Example #22
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static <T> KryoSerializerSnapshotData<T> createFrom(DataInputView in, ClassLoader cl) throws IOException {
	Class<T> typeClass = readTypeClass(in, cl);
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations = readKryoRegistrations(in, cl);
	LinkedOptionalMap<Class<?>, SerializableSerializer<?>> defaultSerializer = readDefaultKryoSerializers(in, cl);
	LinkedOptionalMap<Class<?>, Class<? extends Serializer<?>>> defaultSerializerClasses =
		readDefaultKryoSerializerClasses(in, cl);

	return new KryoSerializerSnapshotData<>(
		typeClass,
		defaultSerializer,
		defaultSerializerClasses,
		kryoRegistrations);
}
 
Example #23
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private KryoSerializerSnapshotData(
	Class<T> typeClass,
	LinkedOptionalMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
	LinkedOptionalMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations) {

	this.typeClass = typeClass;
	this.defaultKryoSerializers = defaultKryoSerializers;
	this.defaultKryoSerializerClasses = defaultKryoSerializerClasses;
	this.kryoRegistrations = kryoRegistrations;
}
 
Example #24
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void writeKryoRegistrations(
	DataOutputView out,
	LinkedOptionalMap<String, KryoRegistration> kryoRegistrations) throws IOException {

	writeOptionalMap(
		out,
		kryoRegistrations,
		DataOutput::writeUTF,
		KryoRegistrationUtil::writeKryoRegistration);
}
 
Example #25
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
private static LinkedOptionalMap<String, KryoRegistration> readKryoRegistrations(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	return readOptionalMap(
		in,
		(stream, unused) -> stream.readUTF(),
		(stream, unused) -> KryoRegistrationUtil.tryReadKryoRegistration(stream, userCodeClassLoader)
	);
}
 
Example #26
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static void writeKryoRegistration(
	DataOutputView out, KryoRegistration kryoRegistration) throws IOException {

	checkNotNull(kryoRegistration);
	out.writeUTF(kryoRegistration.getRegisteredClass().getName());

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		kryoRegistration.getSerializerDefinitionType();

	out.writeInt(serializerDefinitionType.ordinal());
	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			// nothing else to write
			break;
		}
		case CLASS: {
			Class<? extends Serializer<?>> serializerClass = kryoRegistration.getSerializerClass();
			assert serializerClass != null;
			out.writeUTF(serializerClass.getName());
			break;
		}
		case INSTANCE: {
			try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) {
				InstantiationUtil.serializeObject(outViewWrapper, kryoRegistration.getSerializableSerializerInstance());
			}
			break;
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example #27
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static KryoRegistration tryReadKryoRegistration(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	String registeredClassname = in.readUTF();
	Class<?> registeredClass;
	try {
		registeredClass = Class.forName(registeredClassname, true, userCodeClassLoader);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered class " + registeredClassname + " for Kryo serialization in classpath;" +
			" using a dummy class as a placeholder.", e);
		return null;
	}

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		KryoRegistration.SerializerDefinitionType.values()[in.readInt()];

	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			return new KryoRegistration(registeredClass);
		}
		case CLASS: {
			return tryReadWithSerializerClass(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		case INSTANCE: {
			return tryReadWithSerializerInstance(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example #28
Source File: KryoSerializerSnapshotData.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static void writeKryoRegistration(
	DataOutputView out, KryoRegistration kryoRegistration) throws IOException {

	checkNotNull(kryoRegistration);
	out.writeUTF(kryoRegistration.getRegisteredClass().getName());

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		kryoRegistration.getSerializerDefinitionType();

	out.writeInt(serializerDefinitionType.ordinal());
	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			// nothing else to write
			break;
		}
		case CLASS: {
			Class<? extends Serializer<?>> serializerClass = kryoRegistration.getSerializerClass();
			assert serializerClass != null;
			out.writeUTF(serializerClass.getName());
			break;
		}
		case INSTANCE: {
			try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) {
				InstantiationUtil.serializeObject(outViewWrapper, kryoRegistration.getSerializableSerializerInstance());
			}
			break;
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example #29
Source File: KryoSerializerSnapshotData.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static <T> KryoSerializerSnapshotData<T> createFrom(
	Class<T> typeClass,
	LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
	LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
	LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	return new KryoSerializerSnapshotData<>(
		typeClass,
		optionalMapOf(defaultKryoSerializers, Class::getName),
		optionalMapOf(defaultKryoSerializerClasses, Class::getName),
		optionalMapOf(kryoRegistrations, Function.identity()));
}
 
Example #30
Source File: KryoSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that takes lists of registered types and their serializers, and resolve
 * them into a single list such that the result will resemble the final registration
 * result in Kryo.
 */
private static LinkedHashMap<String, KryoRegistration> buildKryoRegistrations(
		Class<?> serializedType,
		LinkedHashSet<Class<?>> registeredTypes,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithSerializerClasses,
		LinkedHashMap<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypesWithSerializers) {

	final LinkedHashMap<String, KryoRegistration> kryoRegistrations = new LinkedHashMap<>();

	kryoRegistrations.put(serializedType.getName(), new KryoRegistration(serializedType));

	for (Class<?> registeredType : checkNotNull(registeredTypes)) {
		kryoRegistrations.put(registeredType.getName(), new KryoRegistration(registeredType));
	}

	for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> registeredTypeWithSerializerClassEntry :
			checkNotNull(registeredTypesWithSerializerClasses).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerClassEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerClassEntry.getKey(),
						registeredTypeWithSerializerClassEntry.getValue()));
	}

	for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypeWithSerializerEntry :
			checkNotNull(registeredTypesWithSerializers).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerEntry.getKey(),
						registeredTypeWithSerializerEntry.getValue()));
	}

	// add Avro support if flink-avro is available; a dummy otherwise
	AvroUtils.getAvroUtils().addAvroGenericDataArrayRegistration(kryoRegistrations);

	return kryoRegistrations;
}