HIGUCHI Yuta
Committed by Yuta HIGUCHI

Check if there's conflict in serializer registration Id.

- remove duplicate detected
- Add little head space, just in case Kryo start registering some more built-ins.

Change-Id: I8b1318d0e11ce1c0680aaef29cc88c96af78ae6f
...@@ -347,7 +347,6 @@ public final class KryoNamespaces { ...@@ -347,7 +347,6 @@ public final class KryoNamespaces {
347 DefaultFlowEntry.class, 347 DefaultFlowEntry.class,
348 StoredFlowEntry.class, 348 StoredFlowEntry.class,
349 DefaultFlowRule.class, 349 DefaultFlowRule.class,
350 - DefaultFlowEntry.class,
351 DefaultPacketRequest.class, 350 DefaultPacketRequest.class,
352 PacketPriority.class, 351 PacketPriority.class,
353 FlowEntry.FlowEntryState.class, 352 FlowEntry.FlowEntryState.class,
...@@ -466,7 +465,6 @@ public final class KryoNamespaces { ...@@ -466,7 +465,6 @@ public final class KryoNamespaces {
466 PartialFailureConstraint.class, 465 PartialFailureConstraint.class,
467 IntentOperation.class, 466 IntentOperation.class,
468 FlowRuleExtPayLoad.class, 467 FlowRuleExtPayLoad.class,
469 - Frequency.class,
470 DefaultAnnotations.class, 468 DefaultAnnotations.class,
471 PortStatistics.class, 469 PortStatistics.class,
472 DefaultPortStatistics.class, 470 DefaultPortStatistics.class,
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onlab.util; 16 package org.onlab.util;
17 17
18 import com.esotericsoftware.kryo.Kryo; 18 import com.esotericsoftware.kryo.Kryo;
19 +import com.esotericsoftware.kryo.Registration;
19 import com.esotericsoftware.kryo.Serializer; 20 import com.esotericsoftware.kryo.Serializer;
20 import com.esotericsoftware.kryo.io.ByteBufferInput; 21 import com.esotericsoftware.kryo.io.ByteBufferInput;
21 import com.esotericsoftware.kryo.io.ByteBufferOutput; 22 import com.esotericsoftware.kryo.io.ByteBufferOutput;
...@@ -59,7 +60,7 @@ public final class KryoNamespace implements KryoFactory, KryoPool { ...@@ -59,7 +60,7 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
59 /** 60 /**
60 * Smallest ID free to use for user defined registrations. 61 * Smallest ID free to use for user defined registrations.
61 */ 62 */
62 - public static final int INITIAL_ID = 11; 63 + public static final int INITIAL_ID = 16;
63 64
64 private static final Logger log = getLogger(KryoNamespace.class); 65 private static final Logger log = getLogger(KryoNamespace.class);
65 66
...@@ -379,6 +380,7 @@ public final class KryoNamespace implements KryoFactory, KryoPool { ...@@ -379,6 +380,7 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
379 */ 380 */
380 @Override 381 @Override
381 public Kryo create() { 382 public Kryo create() {
383 + log.trace("Creating Kryo instance for {}", this);
382 Kryo kryo = new Kryo(); 384 Kryo kryo = new Kryo();
383 kryo.setRegistrationRequired(registrationRequired); 385 kryo.setRegistrationRequired(registrationRequired);
384 386
...@@ -392,15 +394,46 @@ public final class KryoNamespace implements KryoFactory, KryoPool { ...@@ -392,15 +394,46 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
392 id = kryo.getNextRegistrationId(); 394 id = kryo.getNextRegistrationId();
393 } 395 }
394 for (Pair<Class<?>, Serializer<?>> entry : block.types()) { 396 for (Pair<Class<?>, Serializer<?>> entry : block.types()) {
395 - final Serializer<?> serializer = entry.getRight(); 397 + register(kryo, entry.getLeft(), entry.getRight(), id++);
398 + }
399 + }
400 + return kryo;
401 + }
402 +
403 + /**
404 + * Register {@code type} and {@code serializer} to {@code kryo} instance.
405 + *
406 + * @param kryo Kryo instance
407 + * @param type type to register
408 + * @param serializer Specific serializer to register or null to use default.
409 + * @param id type registration id to use
410 + */
411 + private static void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) {
412 + Registration existing = kryo.getRegistration(id);
413 + if (existing != null) {
414 + if (existing.getType() != type) {
415 + log.error("Failed to register {} as {}, {} was already registered.",
416 + type, id, existing.getType());
417 +
418 + throw new IllegalStateException(String.format(
419 + "Failed to register %s as %s, %s was already registered.",
420 + type, id, existing.getType()));
421 + }
422 + // falling through to register call for now.
423 + // Consider skipping, if there's reasonable
424 + // way to compare serializer equivalence.
425 + }
426 + Registration r;
396 if (serializer == null) { 427 if (serializer == null) {
397 - kryo.register(entry.getLeft(), id++); 428 + r = kryo.register(type, id);
398 } else { 429 } else {
399 - kryo.register(entry.getLeft(), serializer, id++); 430 + r = kryo.register(type, serializer, id);
400 } 431 }
432 + if (r.getId() != id) {
433 + log.warn("{} already registed as {}. Skipping {}.",
434 + r.getType(), r.getId(), id);
401 } 435 }
402 - } 436 + log.trace("{} registered as {}", r.getType(), r.getId());
403 - return kryo;
404 } 437 }
405 438
406 @Override 439 @Override
......