Do not fall back to default Kryo serializers 26/104726/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Mar 2023 14:16:40 +0000 (15:16 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Mar 2023 14:46:11 +0000 (15:46 +0100)
Prevent the use of default serializers, so that users have to supply
them. This is the only way we want to be using this facility.

JIRA: CONTROLLER-2072
Change-Id: I69ace2f966e7f1eeeeaff5385ee3389bdaa8d25a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-akka-segmented-journal/src/main/java/org/opendaylight/controller/akka/segjournal/SegmentedJournalActor.java
third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/Namespace.java
third-party/atomix/storage/src/test/java/io/atomix/storage/journal/AbstractJournalTest.java
third-party/atomix/storage/src/test/java/io/atomix/storage/journal/TestEntrySerializer.java [new file with mode: 0644]

index 74fdf2387ef37aef8a91f697584573825e36062f..67c87f1658261fb530ac66dd9cf4cb37977bf5fe 100644 (file)
@@ -18,6 +18,7 @@ import com.codahale.metrics.Histogram;
 import com.codahale.metrics.Meter;
 import com.codahale.metrics.MetricRegistry;
 import com.codahale.metrics.Timer;
+import com.esotericsoftware.kryo.serializers.DefaultSerializers.LongSerializer;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Stopwatch;
 import io.atomix.storage.journal.Indexed;
@@ -147,7 +148,9 @@ final class SegmentedJournalActor extends AbstractActor {
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(SegmentedJournalActor.class);
-    private static final Namespace DELETE_NAMESPACE = Namespace.builder().register(Long.class).build();
+    private static final Namespace DELETE_NAMESPACE = Namespace.builder()
+        .register(new LongSerializer(), Long.class)
+        .build();
     private static final int DELETE_SEGMENT_SIZE = 64 * 1024;
 
     private final String persistenceId;
index 1d460f6532a8e981e50934ec3457bb72febf4bb6..f62ba084ca901fa339e6d35aeb1cb67dc6d2c8bb 100644 (file)
@@ -151,19 +151,6 @@ public final class Namespace implements KryoFactory, KryoPool {
       return this;
     }
 
-    /**
-     * Registers classes to be serialized using Kryo default serializer.
-     *
-     * @param expectedTypes list of classes
-     * @return this
-     */
-    public Builder register(final Class<?>... expectedTypes) {
-      for (Class<?> clazz : expectedTypes) {
-        types.add(Pair.of(new Class<?>[]{clazz}, null));
-      }
-      return this;
-    }
-
     /**
      * Registers serializer for the given set of classes.
      * <p>
index 6c03b922f7db843a1e18cc139014f1918c286379..eaddd3d28d28716591148f7a30a920d01af60a42 100644 (file)
@@ -15,6 +15,7 @@
  */
 package io.atomix.storage.journal;
 
+import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.ByteArraySerializer;
 import io.atomix.utils.serializer.Namespace;
 import org.junit.After;
 import org.junit.Before;
@@ -46,8 +47,8 @@ import static org.junit.Assert.assertTrue;
 @RunWith(Parameterized.class)
 public abstract class AbstractJournalTest {
   private static final Namespace NAMESPACE = Namespace.builder()
-      .register(TestEntry.class)
-      .register(byte[].class)
+      .register(new TestEntrySerializer(), TestEntry.class)
+      .register(new ByteArraySerializer(), byte[].class)
       .build();
 
   protected static final TestEntry ENTRY = new TestEntry(32);
diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/journal/TestEntrySerializer.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/journal/TestEntrySerializer.java
new file mode 100644 (file)
index 0000000..7258286
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * Licensed 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.atomix.storage.journal;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.ByteArraySerializer;
+
+class TestEntrySerializer extends Serializer<TestEntry> {
+    private static final ByteArraySerializer BA_SERIALIZER = new ByteArraySerializer();
+
+    @Override
+    public void write(Kryo kryo, Output output, TestEntry object) {
+        kryo.writeObjectOrNull(output, object.bytes(), BA_SERIALIZER);
+    }
+
+    @Override
+    public TestEntry read(Kryo kryo, Input input, Class<TestEntry> type) {
+        return new TestEntry(kryo.readObjectOrNull(input, byte[].class, BA_SERIALIZER));
+    }
+}