Skip to content

Commit

Permalink
add forward/backward compatible serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed May 10, 2023
1 parent a970d46 commit 6c465fa
Show file tree
Hide file tree
Showing 6 changed files with 1,685 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 The Fury authors
* 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 io.fury.exception;

import io.fury.serializer.CompatibleMode;

/**
* Exception for class compatibility. If {@link CompatibleMode#COMPATIBLE} is not enabled, and the
* class when serializing is different from deserialization, for example, the class add/delete some
* fields, this exception will be thrown.
*
* @author chaokunyang
*/
public class ClassNotCompatibleException extends FuryException {
public ClassNotCompatibleException(String message) {
super(message);
}

public ClassNotCompatibleException(String message, Throwable cause) {
super(message, cause);
}
}
13 changes: 13 additions & 0 deletions java/fury-core/src/main/java/io/fury/resolver/ClassResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private static class ExtRegistry {
// avoid potential recursive call for seq codec generation.
// ex. A->field1: B, B.field1: A
private final Set<Class<?>> getClassCtx = new HashSet<>();
private final Map<Class<?>, FieldResolver> fieldResolverMap = new HashMap<>();
// TODO(chaokunyang) Better to use soft reference, see ObjectStreamClass.
private final ConcurrentHashMap<Tuple2<Class<?>, Boolean>, SortedMap<Field, Descriptor>>
descriptorsCache = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -651,6 +652,18 @@ public Class<? extends Serializer> getJavaSerializer(Class<?> clz) {
return JavaSerializer.class;
}

public FieldResolver getFieldResolver(Class<?> cls) {
// can't use computeIfAbsent, since there may be recursive muiltple
// `getFieldResolver` thus multiple updates, which cause concurrent
// modification exeption.
FieldResolver fieldResolver = extRegistry.fieldResolverMap.get(cls);
if (fieldResolver == null) {
fieldResolver = FieldResolver.of(fury, cls);
extRegistry.fieldResolverMap.put(cls, fieldResolver);
}
return fieldResolver;
}

// thread safe
public SortedMap<Field, Descriptor> getAllDescriptorsMap(Class<?> clz, boolean searchParent) {
// when jit thread query this, it is already built by serialization main thread.
Expand Down
Loading

0 comments on commit 6c465fa

Please sign in to comment.