X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=third-party%2Fatomix%2Fstorage%2Fsrc%2Fmain%2Fjava%2Fio%2Fatomix%2Futils%2Fserializer%2FNamespace.java;h=dcad23d9f0f211332f58f7a32cb6452e8ae6bb8f;hb=64ba24e9c6064e51140cc42d67f312ae3ca7382b;hp=f62ba084ca901fa339e6d35aeb1cb67dc6d2c8bb;hpb=4655e7278357e5b14c86bc24d61671d978475d47;p=controller.git diff --git a/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/Namespace.java b/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/Namespace.java index f62ba084ca..dcad23d9f0 100644 --- a/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/Namespace.java +++ b/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/Namespace.java @@ -23,10 +23,8 @@ import com.esotericsoftware.kryo.io.ByteBufferOutput; import com.esotericsoftware.kryo.pool.KryoCallback; import com.esotericsoftware.kryo.pool.KryoFactory; import com.esotericsoftware.kryo.pool.KryoPool; -import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; -import org.apache.commons.lang3.tuple.Pair; import org.objenesis.strategy.StdInstantiatorStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +36,8 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import static java.util.Objects.requireNonNull; @@ -55,33 +55,21 @@ public final class Namespace implements KryoFactory, KryoPool { */ public static final int DEFAULT_BUFFER_SIZE = 4096; - /** - * Maximum allowed buffer size. - */ - public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000; - /** * ID to use if this KryoNamespace does not define registration id. */ - public static final int FLOATING_ID = -1; + private static final int FLOATING_ID = -1; /** * Smallest ID free to use for user defined registrations. */ - public static final int INITIAL_ID = 16; + private static final int INITIAL_ID = 16; static final String NO_NAME = "(no name)"; private static final Logger LOGGER = LoggerFactory.getLogger(Namespace.class); - /** - * Default Kryo namespace. - */ - public static final Namespace DEFAULT = builder().build(); - - private final KryoPool kryoPool = new KryoPool.Builder(this) - .softReferences() - .build(); + private final KryoPool kryoPool = new KryoPool.Builder(this).softReferences().build(); private final KryoOutputPool kryoOutputPool = new KryoOutputPool(); private final KryoInputPool kryoInputPool = new KryoInputPool(); @@ -89,8 +77,6 @@ public final class Namespace implements KryoFactory, KryoPool { private final ImmutableList registeredBlocks; private final ClassLoader classLoader; - private final boolean compatible; - private final boolean registrationRequired; private final String friendlyName; /** @@ -99,11 +85,9 @@ public final class Namespace implements KryoFactory, KryoPool { //@NotThreadSafe public static final class Builder { private int blockHeadId = INITIAL_ID; - private List[], Serializer>> types = new ArrayList<>(); + private List[], Serializer>> types = new ArrayList<>(); private List blocks = new ArrayList<>(); private ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - private boolean registrationRequired = true; - private boolean compatible = false; /** * Builds a {@link Namespace} instance. @@ -124,31 +108,7 @@ public final class Namespace implements KryoFactory, KryoPool { if (!types.isEmpty()) { blocks.add(new RegistrationBlock(this.blockHeadId, types)); } - return new Namespace(blocks, classLoader, registrationRequired, compatible, friendlyName).populate(1); - } - - /** - * Sets the next Kryo registration Id for following register entries. - * - * @param id Kryo registration Id - * @return this - * @see Kryo#register(Class, Serializer, int) - */ - public Builder nextId(final int id) { - if (!types.isEmpty()) { - if (id != FLOATING_ID && id < blockHeadId + types.size()) { - - if (LOGGER.isWarnEnabled()) { - LOGGER.warn("requested nextId {} could potentially overlap " - + "with existing registrations {}+{} ", - id, blockHeadId, types.size(), new RuntimeException()); - } - } - blocks.add(new RegistrationBlock(this.blockHeadId, types)); - types = new ArrayList<>(); - } - this.blockHeadId = id; - return this; + return new Namespace(blocks, classLoader, friendlyName); } /** @@ -162,7 +122,7 @@ public final class Namespace implements KryoFactory, KryoPool { * @return this */ public Builder register(Serializer serializer, final Class... classes) { - types.add(Pair.of(classes, requireNonNull(serializer))); + types.add(Map.entry(classes, serializer)); return this; } @@ -176,32 +136,6 @@ public final class Namespace implements KryoFactory, KryoPool { this.classLoader = classLoader; return this; } - - /** - * Sets whether backwards/forwards compatible versioned serialization is enabled. - *

- * When compatible serialization is enabled, the {@link CompatibleFieldSerializer} will be set as the - * default serializer for types that do not otherwise explicitly specify a serializer. - * - * @param compatible whether versioned serialization is enabled - * @return this - */ - public Builder setCompatible(boolean compatible) { - this.compatible = compatible; - return this; - } - - /** - * Sets the registrationRequired flag. - * - * @param registrationRequired Kryo's registrationRequired flag - * @return this - * @see Kryo#setRegistrationRequired(boolean) - */ - public Builder setRegistrationRequired(boolean registrationRequired) { - this.registrationRequired = registrationRequired; - return this; - } } /** @@ -218,34 +152,18 @@ public final class Namespace implements KryoFactory, KryoPool { * * @param registeredTypes types to register * @param registrationRequired whether registration is required - * @param compatible whether compatible serialization is enabled * @param friendlyName friendly name for the namespace */ private Namespace( final List registeredTypes, ClassLoader classLoader, - boolean registrationRequired, - boolean compatible, String friendlyName) { this.registeredBlocks = ImmutableList.copyOf(registeredTypes); - this.registrationRequired = registrationRequired; this.classLoader = classLoader; - this.compatible = compatible; this.friendlyName = requireNonNull(friendlyName); - } - - /** - * Populates the Kryo pool. - * - * @param instances to add to the pool - * @return this - */ - public Namespace populate(int instances) { - for (int i = 0; i < instances; ++i) { - release(create()); - } - return this; + // Pre-populate with a single instance + release(create()); } /** @@ -390,21 +308,6 @@ public final class Namespace implements KryoFactory, KryoPool { } } - private String friendlyName() { - return friendlyName; - } - - /** - * Gets the number of classes registered in this Kryo namespace. - * - * @return size of namespace - */ - public int size() { - return (int) registeredBlocks.stream() - .flatMap(block -> block.types().stream()) - .count(); - } - /** * Creates a Kryo instance. * @@ -415,12 +318,7 @@ public final class Namespace implements KryoFactory, KryoPool { LOGGER.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setClassLoader(classLoader); - kryo.setRegistrationRequired(registrationRequired); - - // If compatible serialization is enabled, override the default serializer. - if (compatible) { - kryo.setDefaultSerializer(CompatibleFieldSerializer::new); - } + kryo.setRegistrationRequired(true); // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( @@ -431,8 +329,8 @@ public final class Namespace implements KryoFactory, KryoPool { if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } - for (Pair[], Serializer> entry : block.types()) { - register(kryo, entry.getLeft(), entry.getRight(), id++); + for (Entry[], Serializer> entry : block.types()) { + register(kryo, entry.getKey(), entry.getValue(), id++); } } return kryo; @@ -459,7 +357,7 @@ public final class Namespace implements KryoFactory, KryoPool { if (!matches) { LOGGER.error("{}: Failed to register {} as {}, {} was already registered.", - friendlyName(), types, id, existing.getType()); + friendlyName, types, id, existing.getType()); throw new IllegalStateException(String.format( "Failed to register %s as %s, %s was already registered.", @@ -482,7 +380,7 @@ public final class Namespace implements KryoFactory, KryoPool { if (r != null) { if (r.getId() != id) { LOGGER.debug("{}: {} already registered as {}. Skipping {}.", - friendlyName(), r.getType(), r.getId(), id); + friendlyName, r.getType(), r.getId(), id); } LOGGER.trace("{} registered as {}", r.getType(), r.getId()); } @@ -520,9 +418,9 @@ public final class Namespace implements KryoFactory, KryoPool { static final class RegistrationBlock { private final int begin; - private final ImmutableList[], Serializer>> types; + private final ImmutableList[], Serializer>> types; - RegistrationBlock(int begin, List[], Serializer>> types) { + RegistrationBlock(int begin, List[], Serializer>> types) { this.begin = begin; this.types = ImmutableList.copyOf(types); } @@ -531,7 +429,7 @@ public final class Namespace implements KryoFactory, KryoPool { return begin; } - public ImmutableList[], Serializer>> types() { + public ImmutableList[], Serializer>> types() { return types; }