You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/12/21 12:00:51 UTC

[GitHub] [ignite-3] SammyVimes commented on a change in pull request #516: IGNITE-16154 Implement (un)marhalling of Externalizable

SammyVimes commented on a change in pull request #516:
URL: https://github.com/apache/ignite-3/pull/516#discussion_r773067713



##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java
##########
@@ -122,10 +119,11 @@ private ClassDescriptor externalizable(int descriptorId, Class<? extends Externa
         checkHasPublicNoArgConstructor(clazz);
 
         return new ClassDescriptor(
-            clazz,
-            descriptorId,
-            Collections.emptyList(),
-            SerializationType.EXTERNALIZABLE
+                clazz,
+                descriptorId,
+                Collections.emptyList(),
+                new Serialization(SerializationType.EXTERNALIZABLE,
+                        hasOverrideSerialization(clazz), hasWriteReplace(clazz), hasReadResolve(clazz))

Review comment:
       Indentation

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java
##########
@@ -161,30 +159,25 @@ private static void checkHasPublicNoArgConstructor(Class<? extends Externalizabl
      * @return Class descriptor.
      */
     private ClassDescriptor serializable(int descriptorId, Class<? extends Serializable> clazz) {
-        Method writeObject = getWriteObject(clazz);
-        Method readObject = getReadObject(clazz);
-        Method readObjectNoData = getReadObjectNoData(clazz);
-
-        boolean overrideSerialization = writeObject != null && readObject != null && readObjectNoData != null;
-
-        Method writeReplace = getWriteReplace(clazz);
-        Method readResolve = getReadResolve(clazz);
-
-        int serializationType = SerializationType.SERIALIZABLE;
+        return new ClassDescriptor(clazz, descriptorId, fields(clazz),
+                new Serialization(SerializationType.SERIALIZABLE,
+                        hasOverrideSerialization(clazz), hasWriteReplace(clazz), hasReadResolve(clazz)));

Review comment:
       indentation

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+
+/**
+ * Default implementation of {@link UserObjectMarshaller}.
+ */
+public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
+    private final ClassDescriptorFactoryContext descriptorRegistry;
+    private final ClassDescriptorFactory descriptorFactory;
+
+    public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) {
+        this.descriptorRegistry = descriptorRegistry;
+        this.descriptorFactory = descriptorFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public MarshalledObject marshal(Object object) throws MarshalException {
+        ClassDescriptor descriptor = getOrCreateDescriptor(object);
+
+        var baos = new ByteArrayOutputStream();
+
+        final List<ClassDescriptor> usedDescriptors;
+        try (var dos = new DataOutputStream(baos)) {
+            writeDescriptorId(descriptor, dos);
+
+            usedDescriptors = writeObject(object, descriptor, dos);
+        } catch (IOException e) {
+            throw new MarshalException("Cannot marshal", e);
+        }
+
+        return new MarshalledObject(baos.toByteArray(), usedDescriptors);
+    }
+
+    private ClassDescriptor getOrCreateDescriptor(Object object) {
+        ClassDescriptor descriptor = descriptorRegistry.getDescriptor(object.getClass());
+        if (descriptor == null) {
+            descriptor = descriptorFactory.create(object.getClass());
+        }
+        return descriptor;
+    }
+
+    private void writeDescriptorId(ClassDescriptor descriptor, DataOutputStream dos) throws IOException {
+        dos.writeInt(descriptor.descriptorId());
+    }
+
+    private List<ClassDescriptor> writeObject(Object object, ClassDescriptor descriptor, DataOutputStream dos)
+            throws IOException, MarshalException {
+        final Object objectToWrite;
+        if ((descriptor.isSerializable() || descriptor.isExternalizable()) && descriptor.hasWriteReplace()) {
+            // TODO: what if non-Externalizable is returned?
+            objectToWrite = applyWriteReplace(object);
+        } else {
+            objectToWrite = object;
+        }
+
+        if (descriptor.isExternalizable()) {
+            return writeExternalizable((Externalizable) objectToWrite, descriptor, dos);
+        } else {
+            throw new UnsupportedOperationException("Not supported yet");
+        }
+    }
+
+    private Object applyWriteReplace(Object object) throws MarshalException {
+        Method writeReplaceMethod;
+        try {
+            writeReplaceMethod = object.getClass().getDeclaredMethod("writeReplace");
+        } catch (NoSuchMethodException e) {
+            throw new MarshalException("writeReplace() was not found on " + object.getClass()
+                    + " even though the descriptor says the class has the method", e);
+        }
+
+        writeReplaceMethod.setAccessible(true);
+
+        try {
+            return writeReplaceMethod.invoke(object);
+        } catch (ReflectiveOperationException e) {
+            throw new MarshalException("writeReplace() invocation failed on " + object, e);
+        }
+
+        // TODO: what if null is returned?
+    }
+
+    private List<ClassDescriptor> writeExternalizable(
+            Externalizable externalizable,
+            ClassDescriptor descriptor,
+            DataOutputStream dos
+    ) throws IOException {
+        byte[] externalizableBytes = externalize(externalizable);
+
+        dos.writeInt(externalizableBytes.length);
+        dos.write(externalizableBytes);
+
+        return List.of(descriptor);
+    }
+
+    private byte[] externalize(Externalizable externalizable) throws IOException {
+        var baos = new ByteArrayOutputStream();
+        try (var oos = new ObjectOutputStream(baos)) {
+            externalizable.writeExternal(oos);
+        }
+
+        return baos.toByteArray();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public <T> T unmarshal(byte[] bytes) throws UnmarshalException {
+        try (var dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
+            int descriptorId = readDescriptorId(dis);
+            ClassDescriptor descriptor = descriptorRegistry.getRequiredDescriptor(descriptorId);
+
+            if (descriptor.isExternalizable()) {
+                Object readObject = readExternalizable(descriptor, dis);
+                Object resultObject;
+                if (descriptor.hasReadResolve()) {
+                    resultObject = applyReadResolve(readObject);
+                } else {
+                    resultObject = readObject;
+                }
+                @SuppressWarnings("unchecked") T castResult = (T) resultObject;

Review comment:
       a comment like //noinspection looks better for a single statement (it's IntelliJ specific though)

##########
File path: modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerTest.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link DefaultUserObjectMarshaller}.
+ */
+class DefaultUserObjectMarshallerTest {
+    private final ClassDescriptorFactoryContext descriptorRegistry = new ClassDescriptorFactoryContext();
+    private final ClassDescriptorFactory descriptorFactory = new ClassDescriptorFactory(descriptorRegistry);
+
+    private final DefaultUserObjectMarshaller marshaller = new DefaultUserObjectMarshaller(descriptorRegistry, descriptorFactory);
+
+    private static final int WRITE_REPLACE_INCREMENT = 1_000_000;
+    private static final int READ_RESOLVE_INCREMENT = 1_000;
+
+    @Test
+    void marshalsExternalizable() throws Exception {
+        MarshalledObject marshalled = marshaller.marshal(new SimpleExternalizable(42));

Review comment:
       Do we need a separate for only marshalling if we have unmarshalsExternalizable?

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+
+/**
+ * Default implementation of {@link UserObjectMarshaller}.
+ */
+public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
+    private final ClassDescriptorFactoryContext descriptorRegistry;
+    private final ClassDescriptorFactory descriptorFactory;
+
+    public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) {
+        this.descriptorRegistry = descriptorRegistry;
+        this.descriptorFactory = descriptorFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public MarshalledObject marshal(Object object) throws MarshalException {
+        ClassDescriptor descriptor = getOrCreateDescriptor(object);
+
+        var baos = new ByteArrayOutputStream();
+
+        final List<ClassDescriptor> usedDescriptors;
+        try (var dos = new DataOutputStream(baos)) {
+            writeDescriptorId(descriptor, dos);
+
+            usedDescriptors = writeObject(object, descriptor, dos);
+        } catch (IOException e) {
+            throw new MarshalException("Cannot marshal", e);
+        }
+
+        return new MarshalledObject(baos.toByteArray(), usedDescriptors);
+    }
+
+    private ClassDescriptor getOrCreateDescriptor(Object object) {
+        ClassDescriptor descriptor = descriptorRegistry.getDescriptor(object.getClass());
+        if (descriptor == null) {
+            descriptor = descriptorFactory.create(object.getClass());
+        }
+        return descriptor;
+    }
+
+    private void writeDescriptorId(ClassDescriptor descriptor, DataOutputStream dos) throws IOException {
+        dos.writeInt(descriptor.descriptorId());
+    }
+
+    private List<ClassDescriptor> writeObject(Object object, ClassDescriptor descriptor, DataOutputStream dos)
+            throws IOException, MarshalException {
+        final Object objectToWrite;
+        if ((descriptor.isSerializable() || descriptor.isExternalizable()) && descriptor.hasWriteReplace()) {
+            // TODO: what if non-Externalizable is returned?

Review comment:
       TODO message should be addressing a ticket

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization;
+
+/**
+ * Describes how a class is to be serialized.
+ */
+public class Serialization {
+    /** Serialization type. */
+    private final SerializationType type;
+
+    /** Whether a Serializable has writeObject()/readObject()/readObjectNoData() methods. */
+    private final boolean hasSerializationOverride;
+    /** Whether a Serializable/Externalizable has writeReplace() method. */
+    private final boolean hasWriteReplace;
+    /** Whether a Serializable/Externalizable has readResolve() method. */
+    private final boolean hasReadResolve;
+
+    /**
+     * Creates a new Serialization.
+     *
+     * @param type                     type
+     * @param hasSerializationOverride whether a Serializable has writeObject()/readObject()/readObjectNoData() methods
+     * @param hasWriteReplace          whether a Serializable/Externalizable has writeReplace() method
+     * @param hasReadResolve           whether a Serializable/Externalizable has readResolve() method
+     */
+    public Serialization(SerializationType type, boolean hasSerializationOverride, boolean hasWriteReplace, boolean hasReadResolve) {
+        this.type = type;
+        this.hasSerializationOverride = hasSerializationOverride;
+        this.hasWriteReplace = hasWriteReplace;
+        this.hasReadResolve = hasReadResolve;
+    }
+
+    /**
+     * Creates a new Serialization with all optional features disabled.
+     *
+     * @param type serialization type
+     */
+    public Serialization(SerializationType type) {
+        this(type, false, false, false);
+    }
+
+    public SerializationType type() {

Review comment:
       Missing javadocs here and for the rest of the methods

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+
+/**
+ * Default implementation of {@link UserObjectMarshaller}.
+ */
+public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
+    private final ClassDescriptorFactoryContext descriptorRegistry;
+    private final ClassDescriptorFactory descriptorFactory;
+
+    public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) {
+        this.descriptorRegistry = descriptorRegistry;
+        this.descriptorFactory = descriptorFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public MarshalledObject marshal(Object object) throws MarshalException {
+        ClassDescriptor descriptor = getOrCreateDescriptor(object);
+
+        var baos = new ByteArrayOutputStream();
+
+        final List<ClassDescriptor> usedDescriptors;
+        try (var dos = new DataOutputStream(baos)) {
+            writeDescriptorId(descriptor, dos);
+
+            usedDescriptors = writeObject(object, descriptor, dos);
+        } catch (IOException e) {
+            throw new MarshalException("Cannot marshal", e);
+        }
+
+        return new MarshalledObject(baos.toByteArray(), usedDescriptors);
+    }
+
+    private ClassDescriptor getOrCreateDescriptor(Object object) {

Review comment:
       Missing javadocs here and after

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization;
+
+/**
+ * Describes how a class is to be serialized.
+ */
+public class Serialization {
+    /** Serialization type. */
+    private final SerializationType type;
+
+    /** Whether a Serializable has writeObject()/readObject()/readObjectNoData() methods. */
+    private final boolean hasSerializationOverride;
+    /** Whether a Serializable/Externalizable has writeReplace() method. */
+    private final boolean hasWriteReplace;
+    /** Whether a Serializable/Externalizable has readResolve() method. */
+    private final boolean hasReadResolve;
+
+    /**
+     * Creates a new Serialization.
+     *
+     * @param type                     type
+     * @param hasSerializationOverride whether a Serializable has writeObject()/readObject()/readObjectNoData() methods
+     * @param hasWriteReplace          whether a Serializable/Externalizable has writeReplace() method
+     * @param hasReadResolve           whether a Serializable/Externalizable has readResolve() method
+     */
+    public Serialization(SerializationType type, boolean hasSerializationOverride, boolean hasWriteReplace, boolean hasReadResolve) {
+        this.type = type;

Review comment:
       Maybe add assertions for things that can't be mixed, like arbitrary with writeReplace and so on?

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/MarshalledObject.java
##########
@@ -0,0 +1,54 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+
+/**
+ * Represents a marshalled object: the marshalled representation with information about how it was marshalled
+ * (including what descriptors were used when marshalling it).
+ */
+public class MarshalledObject {
+    private final byte[] bytes;
+    private final List<ClassDescriptor> usedDescriptors;

Review comment:
       No javadocs and no newlines between fields

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+
+/**
+ * Default implementation of {@link UserObjectMarshaller}.
+ */
+public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
+    private final ClassDescriptorFactoryContext descriptorRegistry;
+    private final ClassDescriptorFactory descriptorFactory;
+
+    public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) {
+        this.descriptorRegistry = descriptorRegistry;
+        this.descriptorFactory = descriptorFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public MarshalledObject marshal(Object object) throws MarshalException {
+        ClassDescriptor descriptor = getOrCreateDescriptor(object);
+
+        var baos = new ByteArrayOutputStream();
+
+        final List<ClassDescriptor> usedDescriptors;
+        try (var dos = new DataOutputStream(baos)) {
+            writeDescriptorId(descriptor, dos);
+
+            usedDescriptors = writeObject(object, descriptor, dos);
+        } catch (IOException e) {
+            throw new MarshalException("Cannot marshal", e);
+        }
+
+        return new MarshalledObject(baos.toByteArray(), usedDescriptors);
+    }
+
+    private ClassDescriptor getOrCreateDescriptor(Object object) {
+        ClassDescriptor descriptor = descriptorRegistry.getDescriptor(object.getClass());
+        if (descriptor == null) {
+            descriptor = descriptorFactory.create(object.getClass());
+        }
+        return descriptor;
+    }
+
+    private void writeDescriptorId(ClassDescriptor descriptor, DataOutputStream dos) throws IOException {
+        dos.writeInt(descriptor.descriptorId());
+    }
+
+    private List<ClassDescriptor> writeObject(Object object, ClassDescriptor descriptor, DataOutputStream dos)
+            throws IOException, MarshalException {
+        final Object objectToWrite;
+        if ((descriptor.isSerializable() || descriptor.isExternalizable()) && descriptor.hasWriteReplace()) {
+            // TODO: what if non-Externalizable is returned?
+            objectToWrite = applyWriteReplace(object);
+        } else {
+            objectToWrite = object;
+        }
+
+        if (descriptor.isExternalizable()) {
+            return writeExternalizable((Externalizable) objectToWrite, descriptor, dos);
+        } else {
+            throw new UnsupportedOperationException("Not supported yet");
+        }
+    }
+
+    private Object applyWriteReplace(Object object) throws MarshalException {
+        Method writeReplaceMethod;
+        try {
+            writeReplaceMethod = object.getClass().getDeclaredMethod("writeReplace");

Review comment:
       I think that can be a bit expensive. Might I suggest storing all this methods in a descriptor and using method handles with `invokeExact`?

##########
File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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 org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+
+/**
+ * Default implementation of {@link UserObjectMarshaller}.
+ */
+public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
+    private final ClassDescriptorFactoryContext descriptorRegistry;
+    private final ClassDescriptorFactory descriptorFactory;
+
+    public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) {
+        this.descriptorRegistry = descriptorRegistry;
+        this.descriptorFactory = descriptorFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public MarshalledObject marshal(Object object) throws MarshalException {
+        ClassDescriptor descriptor = getOrCreateDescriptor(object);
+
+        var baos = new ByteArrayOutputStream();
+
+        final List<ClassDescriptor> usedDescriptors;
+        try (var dos = new DataOutputStream(baos)) {
+            writeDescriptorId(descriptor, dos);
+
+            usedDescriptors = writeObject(object, descriptor, dos);
+        } catch (IOException e) {
+            throw new MarshalException("Cannot marshal", e);
+        }
+
+        return new MarshalledObject(baos.toByteArray(), usedDescriptors);
+    }
+
+    private ClassDescriptor getOrCreateDescriptor(Object object) {
+        ClassDescriptor descriptor = descriptorRegistry.getDescriptor(object.getClass());
+        if (descriptor == null) {
+            descriptor = descriptorFactory.create(object.getClass());
+        }
+        return descriptor;
+    }
+
+    private void writeDescriptorId(ClassDescriptor descriptor, DataOutputStream dos) throws IOException {
+        dos.writeInt(descriptor.descriptorId());
+    }
+
+    private List<ClassDescriptor> writeObject(Object object, ClassDescriptor descriptor, DataOutputStream dos)
+            throws IOException, MarshalException {
+        final Object objectToWrite;
+        if ((descriptor.isSerializable() || descriptor.isExternalizable()) && descriptor.hasWriteReplace()) {
+            // TODO: what if non-Externalizable is returned?
+            objectToWrite = applyWriteReplace(object);
+        } else {
+            objectToWrite = object;
+        }
+
+        if (descriptor.isExternalizable()) {
+            return writeExternalizable((Externalizable) objectToWrite, descriptor, dos);
+        } else {
+            throw new UnsupportedOperationException("Not supported yet");
+        }
+    }
+
+    private Object applyWriteReplace(Object object) throws MarshalException {
+        Method writeReplaceMethod;
+        try {
+            writeReplaceMethod = object.getClass().getDeclaredMethod("writeReplace");
+        } catch (NoSuchMethodException e) {
+            throw new MarshalException("writeReplace() was not found on " + object.getClass()
+                    + " even though the descriptor says the class has the method", e);
+        }
+
+        writeReplaceMethod.setAccessible(true);
+
+        try {
+            return writeReplaceMethod.invoke(object);
+        } catch (ReflectiveOperationException e) {
+            throw new MarshalException("writeReplace() invocation failed on " + object, e);
+        }
+
+        // TODO: what if null is returned?
+    }
+
+    private List<ClassDescriptor> writeExternalizable(
+            Externalizable externalizable,
+            ClassDescriptor descriptor,
+            DataOutputStream dos
+    ) throws IOException {
+        byte[] externalizableBytes = externalize(externalizable);
+
+        dos.writeInt(externalizableBytes.length);
+        dos.write(externalizableBytes);
+
+        return List.of(descriptor);
+    }
+
+    private byte[] externalize(Externalizable externalizable) throws IOException {
+        var baos = new ByteArrayOutputStream();
+        try (var oos = new ObjectOutputStream(baos)) {
+            externalizable.writeExternal(oos);
+        }
+
+        return baos.toByteArray();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public <T> T unmarshal(byte[] bytes) throws UnmarshalException {
+        try (var dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
+            int descriptorId = readDescriptorId(dis);
+            ClassDescriptor descriptor = descriptorRegistry.getRequiredDescriptor(descriptorId);
+
+            if (descriptor.isExternalizable()) {
+                Object readObject = readExternalizable(descriptor, dis);
+                Object resultObject;
+                if (descriptor.hasReadResolve()) {
+                    resultObject = applyReadResolve(readObject);
+                } else {
+                    resultObject = readObject;
+                }
+                @SuppressWarnings("unchecked") T castResult = (T) resultObject;
+                return castResult;
+            } else {
+                throw new UnsupportedOperationException("Not supported yet");
+            }
+        } catch (IOException | ClassNotFoundException e) {
+            throw new UnmarshalException("Cannot unmarshal", e);
+        }
+    }
+
+    private int readDescriptorId(DataInputStream dis) throws IOException {
+        return dis.readInt();
+    }
+
+    private <T extends Externalizable> T readExternalizable(ClassDescriptor descriptor, DataInputStream dis)
+            throws IOException, ClassNotFoundException, UnmarshalException {
+        T object = instantiateObject(descriptor);
+
+        int length = dis.readInt();
+        byte[] bytes = new byte[length];
+        dis.readFully(bytes);
+
+        try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+            object.readExternal(ois);
+        }
+
+        return object;
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T extends Externalizable> T instantiateObject(ClassDescriptor descriptor) throws UnmarshalException {
+        try {
+            return (T) descriptor.clazz().getConstructor().newInstance();
+        } catch (ReflectiveOperationException e) {
+            throw new UnmarshalException("Cannot instantiate " + descriptor.clazz(), e);
+        }
+    }
+
+    private Object applyReadResolve(Object object) throws UnmarshalException {
+        Method readResolveMethod;
+        try {
+            readResolveMethod = object.getClass().getDeclaredMethod("readResolve");
+        } catch (NoSuchMethodException e) {
+            throw new UnmarshalException("readResolve() was not found on " + object.getClass()
+                    + " even though the descriptor says the class has the method", e);
+        }
+
+        readResolveMethod.setAccessible(true);
+
+        try {
+            return readResolveMethod.invoke(object);
+        } catch (ReflectiveOperationException e) {
+            throw new UnmarshalException("readResolve() invocation failed on " + object, e);
+        }
+
+        // TODO: what if null is returned?

Review comment:
       TODO without a ticket




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org