com.esotericsoftware.kryo.Registration Java Examples

The following examples show how to use com.esotericsoftware.kryo.Registration. 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: AvroKryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #2
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
public Registration readClass(Input input) {
	int classID = input.readVarInt(true);
	switch (classID) {
	case Kryo.NULL:
		if (TRACE || (DEBUG && kryo.getDepth() == 1))
			log("Read", null);
		return null;
	case NAME + 2: // Offset for NAME and NULL.
		return readName(input);
	}
	if (classID == memoizedClassId)
		return memoizedClassIdValue;
	Registration registration = idToRegistration.get(classID - 2);
	if (registration == null)
		throw new KryoException("Encountered unregistered class ID: " + (classID - 2));
	if (TRACE)
		trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType()));
	memoizedClassId = classID;
	memoizedClassIdValue = registration;
	return registration;
}
 
Example #3
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
public Registration writeClass(Output output, Class type) {
	if (type == null) {
		if (TRACE || (DEBUG && kryo.getDepth() == 1))
			log("Write", null);
		output.writeVarInt(Kryo.NULL, true);
		return null;
	}
	Registration registration = kryo.getRegistration(type);
	if (registration.getId() == NAME)
		writeName(output, type, registration);
	else {
		if (TRACE)
			trace("kryo", "Write class " + registration.getId() + ": " + className(type));
		output.writeVarInt(registration.getId() + 2, true);
	}
	return registration;
}
 
Example #4
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
private Tuple2<Registration, Integer> getRegistration(Class<?> type) {
	if (childRegistrationMap == null) {
		childRegistrationMap = new HashMap<>();
	}

	Tuple2<Registration, Integer> childRegistration = childRegistrationMap.get(type);
	if (childRegistration != null) {
		return childRegistration;
	}

	Registration registration = kryo.getRegistration(type);
	childRegistration = Tuple.tuple(registration, childRegistrationMap.size());

	childRegistrationMap.put(type, childRegistration);

	return childRegistration;
}
 
Example #5
Source File: AvroKryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #6
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
public Registration register(Registration registration) {
	if (registration == null)
		throw new IllegalArgumentException("registration cannot be null.");
	if (registration.getId() != NAME) {
		if (TRACE) {
			trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType())
					+ " (" + registration.getSerializer().getClass().getName() + ")");
		}
		idToRegistration.put(registration.getId(), registration);
	} else if (TRACE) {
		trace("kryo", "Register class name: " + className(registration.getType()) + " ("
				+ registration.getSerializer().getClass().getName() + ")");
	}
	classToRegistration.put(registration.getType(), registration);
	if (registration.getType().isPrimitive())
		classToRegistration.put(getWrapperClass(registration.getType()), registration);
	return registration;
}
 
Example #7
Source File: KryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #8
Source File: AvroKryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #9
Source File: AvroKryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #10
Source File: KryoSerializerRegistrationsTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #11
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 快速写入
 * 
 * @param kryo
 * @param output
 * @param defaultRegistration
 * @param value
 */
public static void fastWrite(Kryo kryo, Output output, Registration defaultRegistration, Object value) {
	if (value == null) {
		kryo.writeClass(output, null);
		return;
	}

	Class<?> type = value.getClass();

	if (defaultRegistration.getType().equals(type)) {
		if (defaultRegistration.getId() == FastClassResolver.NAME) {
			((FastClassResolver) kryo.getClassResolver()).writeName(output, type, defaultRegistration);
		} else {
			output.writeVarInt(defaultRegistration.getId() + 2, true);
		}

		kryo.writeObject(output, value, defaultRegistration.getSerializer());
	} else {
		Registration registration = kryo.writeClass(output, value.getClass());
		Serializer<?> serializer = registration.getSerializer();
		kryo.writeObject(output, value, serializer);
	}
}
 
Example #12
Source File: AvroKryoSerializerRegistrationsTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #13
Source File: AvroKryoSerializerRegistrationsTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #14
Source File: KryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kryo serializer and writes the default registrations out to a
 * comma separated file with one entry per line:
 *
 * <pre>
 * id,class
 * </pre>
 *
 * <p>The produced file is used to check that the registered IDs don't change
 * in future Flink versions.
 *
 * <p>This method is not used in the tests, but documents how the test file
 * has been created and can be used to re-create it if needed.
 *
 * @param filePath File path to write registrations to
 */
private void writeDefaultKryoRegistrations(String filePath) throws IOException {
	final File file = new File(filePath);
	if (file.exists()) {
		assertTrue(file.delete());
	}

	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();
	final int nextId = kryo.getNextRegistrationId();

	try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
		for (int i = 0; i < nextId; i++) {
			Registration registration = kryo.getRegistration(i);
			String str = registration.getId() + "," + registration.getType().getName();
			writer.write(str, 0, str.length());
			writer.newLine();
		}

		System.out.println("Created file with registrations at " + file.getAbsolutePath());
	}
}
 
Example #15
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object slowRead(Kryo kryo, Input input) {
	Registration registration = kryo.readClass(input);
	if (registration == null) {
		return null;
	} else {
		Serializer<?> serializer = registration.getSerializer();
		return kryo.readObject(input, registration.getType(), serializer);
	}
}
 
Example #16
Source File: KryoCoderProvider.java    From beam with Apache License 2.0 5 votes vote down vote up
private <T> boolean hasUserProvidedRegistration(TypeDescriptor<T> typeDescriptor) {
  final KryoState kryoState = KryoState.get(coder);
  final Class<? super T> rawType = typeDescriptor.getRawType();
  final Kryo kryo = kryoState.getKryo();
  final ClassResolver classResolver = kryo.getClassResolver();
  final Registration registration = classResolver.getRegistration(rawType);
  return registration != null && registration.getId() >= kryoState.getFirstRegistrationId();
}
 
Example #17
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
protected Registration readName(Input input) {
	int nameId = input.readVarInt(true);
	if (nameIdToClass == null)
		nameIdToClass = new IntToObjectArrayMap<>();
	Class type = nameIdToClass.get(nameId);
	if (type == null) {
		// Only read the class name the first time encountered in object graph.
		String className = input.readString();
		type = getTypeByName(className);
		if (type == null) {
			try {
				type = Class.forName(className, false, kryo.getClassLoader());
			} catch (ClassNotFoundException ex) {
				if (WARN)
					warn("kryo", "Unable to load class " + className
							+ " with kryo's ClassLoader. Retrying with current..");
				try {
					type = Class.forName(className);
				} catch (ClassNotFoundException e) {
					throw new KryoException("Unable to find class: " + className, ex);
				}
			}
			if (nameToClass == null)
				nameToClass = new FastMap<>(32, 0.5F);
			nameToClass.put(className, type);
		}
		nameIdToClass.put(nameId, type);
		if (TRACE)
			trace("kryo", "Read class name: " + className);
	} else {
		if (TRACE)
			trace("kryo", "Read class name reference " + nameId + ": " + className(type));
	}
	return kryo.getRegistration(type);
}
 
Example #18
Source File: SparkRunnerKryoRegistratorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void registerClasses(Kryo kryo) {
  super.registerClasses(kryo);
  Registration registration = kryo.getRegistration(MicrobatchSource.class);
  com.esotericsoftware.kryo.Serializer kryoSerializer = registration.getSerializer();
  assertTrue(kryoSerializer instanceof StatelessJavaSerializer);
}
 
Example #19
Source File: DefaultStatefulStreamCodec.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public Registration registerImplicit(Class type)
{
  while (getRegistration(nextAvailableRegistrationId) != null) {
    nextAvailableRegistrationId++;
  }

  //logger.debug("adding new classid pair {} => {}", nextAvailableRegistrationId, type.getName());
  pairs.add(new ClassIdPair(nextAvailableRegistrationId, type.getName()));
  return register(new Registration(type, kryo.getDefaultSerializer(type), nextAvailableRegistrationId++));
}
 
Example #20
Source File: DefaultStatefulStreamCodec.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public void registerExplicit(ClassIdPair pair) throws ClassNotFoundException
{
  //logger.debug("registering class {} => {}", pair.classname, pair.id);
  //pairs.add(pair);
  Class type = Class.forName(pair.classname, false, Thread.currentThread().getContextClassLoader());
  register(new Registration(type, kryo.getDefaultSerializer(type), pair.id));
  if (nextAvailableRegistrationId <= pair.id) {
    nextAvailableRegistrationId = pair.id + 1;
  }
}
 
Example #21
Source File: KryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (registeredClass.equals("org.apache.avro.generic.GenericData$Array")) {
				// starting with Flink 1.4 Avro is no longer a dependency of core. Avro is
				// only available if flink-avro is present. There is a special version of
				// this test in AvroKryoSerializerRegistrationsTest that verifies correct
				// registration of Avro types if present
				assertThat(
					registration.getType().getName(),
					is("org.apache.flink.api.java.typeutils.runtime.kryo.Serializers$DummyAvroRegisteredClass"));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #22
Source File: GATKRegistrator.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Make sure that all FuncotationMap (which incl. all Funcotation concrete classes and members) classes are registered
 *  to support {@link org.broadinstitute.hellbender.tools.funcotator.FuncotationMap#create(FuncotationMap)}
 *
 * @param kryo Kryo instance to update in-place.  Never {@code null}
 */
@VisibleForTesting
public static void registerFuncotationMapDependencies(final Kryo kryo) {
    Utils.nonNull(kryo);
    Registration registration = kryo.register(TableFuncotation.class);
    registration.setInstantiator(new ObjectInstantiator<TableFuncotation>() {
        public TableFuncotation newInstance() {
            return TableFuncotation.create(new LinkedHashMap<>(), Allele.UNSPECIFIED_ALTERNATE_ALLELE, "TEMP", null);
        }
    });
    registration = kryo.register(VcfFuncotationMetadata.class);
    registration.setInstantiator(new ObjectInstantiator<VcfFuncotationMetadata>() {
        public VcfFuncotationMetadata newInstance() {
            return VcfFuncotationMetadata.create(new ArrayList<>());
        }
    });
    registration = kryo.register(VCFInfoHeaderLine.class);
    registration.setInstantiator(new ObjectInstantiator<VCFInfoHeaderLine>() {
        public VCFInfoHeaderLine newInstance() {
            return new VCFInfoHeaderLine("TMP", 2, VCFHeaderLineType.String, "");
        }
    });
    registration = kryo.register(Allele.class);
    registration.setInstantiator(new ObjectInstantiator<Allele>() {
        public Allele newInstance() {
            return Allele.create("TCGA");
        }
    });
}
 
Example #23
Source File: FastClassResolver.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
public Registration getRegistration(Class type) {
	if (type == memoizedClass)
		return memoizedClassValue;
	Registration registration = classToRegistration.get(type);
	if (registration != null) {
		memoizedClass = type;
		memoizedClassValue = registration;
	}
	return registration;
}
 
Example #24
Source File: KryoSerializerRegistrationsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (registeredClass.equals("org.apache.avro.generic.GenericData$Array")) {
				// starting with Flink 1.4 Avro is no longer a dependency of core. Avro is
				// only available if flink-avro is present. There is a special version of
				// this test in AvroKryoSerializerRegistrationsTest that verifies correct
				// registration of Avro types if present
				assertThat(
					registration.getType().getName(),
					is("org.apache.flink.api.java.typeutils.runtime.kryo.Serializers$DummyAvroRegisteredClass"));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #25
Source File: KryoSerializerRegistrationsTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the registered classes in Kryo did not change.
 *
 * <p>Once we have proper serializer versioning this test will become obsolete.
 * But currently a change in the serializers can break savepoint backwards
 * compatibility between Flink versions.
 */
@Test
public void testDefaultKryoRegisteredClassesDidNotChange() throws Exception {
	final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo();

	try (BufferedReader reader = new BufferedReader(new InputStreamReader(
			getClass().getClassLoader().getResourceAsStream("flink_11-kryo_registrations")))) {

		String line;
		while ((line = reader.readLine()) != null) {
			String[] split = line.split(",");
			final int tag = Integer.parseInt(split[0]);
			final String registeredClass = split[1];

			Registration registration = kryo.getRegistration(tag);

			if (registration == null) {
				fail(String.format("Registration for %d = %s got lost", tag, registeredClass));
			}
			else if (registeredClass.equals("org.apache.avro.generic.GenericData$Array")) {
				// starting with Flink 1.4 Avro is no longer a dependency of core. Avro is
				// only available if flink-avro is present. There is a special version of
				// this test in AvroKryoSerializerRegistrationsTest that verifies correct
				// registration of Avro types if present
				assertThat(
					registration.getType().getName(),
					is("org.apache.flink.api.java.typeutils.runtime.kryo.Serializers$DummyAvroRegisteredClass"));
			}
			else if (!registeredClass.equals(registration.getType().getName())) {
				fail(String.format("Registration for %d = %s changed to %s",
						tag, registeredClass, registration.getType().getName()));
			}
		}
	}
}
 
Example #26
Source File: DefaultStatefulStreamCodec.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void registerExplicit(ClassIdPair pair) throws ClassNotFoundException
{
  //logger.debug("registering class {} => {}", pair.classname, pair.id);
  //pairs.add(pair);
  Class type = Class.forName(pair.classname, false, Thread.currentThread().getContextClassLoader());
  register(new Registration(type, kryo.getDefaultSerializer(type), pair.id));
  if (nextAvailableRegistrationId <= pair.id) {
    nextAvailableRegistrationId = pair.id + 1;
  }
}
 
Example #27
Source File: DefaultStatefulStreamCodec.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public Registration registerImplicit(Class type)
{
  while (getRegistration(nextAvailableRegistrationId) != null) {
    nextAvailableRegistrationId++;
  }

  //logger.debug("adding new classid pair {} => {}", nextAvailableRegistrationId, type.getName());
  pairs.add(new ClassIdPair(nextAvailableRegistrationId, type.getName()));
  return register(new Registration(type, kryo.getDefaultSerializer(type), nextAvailableRegistrationId++));
}
 
Example #28
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
private void appendReadFieldCode(StringBuilder builder, Field field) {
	Class<?> type = field.getType();

	if (type == int.class) {
		builder.append("input.readInt(false)");
		return;
	}

	if (type == String.class) {
		builder.append("input.readString()");
		return;
	}

	if (type == long.class) {
		builder.append("input.readLong(false)");
		return;
	}

	if (type == boolean.class) {
		builder.append("input.readBoolean()");
		return;
	}

	if (type == double.class) {
		builder.append("input.readDouble()");
		return;
	}

	if (type == float.class) {
		builder.append("input.readFloat()");
		return;
	}

	if (type == short.class) {
		builder.append("input.readShort()");
		return;
	}

	if (type == byte.class) {
		builder.append("input.readByte()");
		return;
	}

	if (type == char.class) {
		builder.append("input.getChar()");
		return;
	}

	// 其他复杂类型
	boolean isFinal = Modifier.isFinal(type.getModifiers());
	Tuple2<Registration, Integer> registration = getRegistration(type);
	int id = registration._2;

	// 强制类型转换
	builder.append("(");
	builder.append(type.getName());
	builder.append(")");

	if (isFinal) {// 不需要写入className
		// kryo.readObjectOrNull(input, type, serializer);
		builder.append("kryo.readObjectOrNull(input, ");
		builder.append(type.getName());
		builder.append(".class, ");
		builder.append(CHILD_REGISTRATION_PREFIX);
		builder.append(id);
		builder.append(".getSerializer())");
	} else {
		// FastSerializer.slowRead(kryo, input);
		builder.append(FastSerializer.class.getName());
		builder.append(".slowRead(kryo, input)");
	}
}
 
Example #29
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
private void appendWriteField(StringBuilder builder, Field field) {
	Class<?> type = field.getType();

	if (type == int.class) {
		// output.writeInt(target.fieldName, false)
		builder.append("output.writeInt(");
		appendGetFieldValue(builder, field);
		builder.append(", false);");
		return;
	}

	if (type == String.class) {
		// output.writeString(target.fieldName)
		builder.append("output.writeString(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == long.class) {
		// output.writeLong(target.fieldName, false)
		builder.append("output.writeLong(");
		appendGetFieldValue(builder, field);
		builder.append(", false);");
		return;
	}

	if (type == boolean.class) {
		// output.writeBoolean(target.fieldName)
		builder.append("output.writeBoolean(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == double.class) {
		// output.writeDouble(target.fieldName)
		builder.append("output.writeDouble(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == float.class) {
		// output.writeFloat(target.fieldName)
		builder.append("output.writeFloat(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == short.class) {
		// output.writeShort(target.fieldName)
		builder.append("output.writeShort(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == byte.class) {
		// output.writeByte(target.fieldName)
		builder.append("output.writeByte(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	if (type == char.class) {
		// output.getChar(target.fieldName)
		builder.append("output.writeChar(");
		appendGetFieldValue(builder, field);
		builder.append(");");
		return;
	}

	// 其他复杂类型
	boolean isFinal = Modifier.isFinal(type.getModifiers());
	Tuple2<Registration, Integer> registration = getRegistration(type);
	int id = registration._2;

	if (isFinal) {// 不需要写入className
		// kryo.writeObjectOrNull(output, value, serializer);
		builder.append("kryo.writeObjectOrNull(output, ");
		appendGetFieldValue(builder, field);
		builder.append(", ");
		builder.append(CHILD_REGISTRATION_PREFIX);
		builder.append(id);
		builder.append(".getSerializer());");
	} else {
		// FastSerializer.fastWrite(kryo, output, defaultRegistration, value);
		builder.append(FastSerializer.class.getName());
		builder.append(".fastWrite(kryo, output, ");
		builder.append(CHILD_REGISTRATION_PREFIX);
		builder.append(id);
		builder.append(", ");
		appendGetFieldValue(builder, field);
		builder.append(");");
	}
}
 
Example #30
Source File: DefaultStatefulStreamCodec.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void unregister(int classId)
{
  Registration registration = idToRegistration.remove(classId);
  classToRegistration.remove(registration.getType());
  getRegistration(int.class); /* make sure that we bust the memoized cache in superclass */
}