You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tl...@apache.org on 2021/07/06 14:10:59 UTC

[ignite] branch ignite-15065 created (now 3281e53)

This is an automated email from the ASF dual-hosted git repository.

tledkov pushed a change to branch ignite-15065
in repository https://gitbox.apache.org/repos/asf/ignite.git.


      at 3281e53  IGNITE-15065 Add an explicit method to register binary type based on class

This branch includes the following new commits:

     new 3281e53  IGNITE-15065 Add an explicit method to register binary type based on class

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[ignite] 01/01: IGNITE-15065 Add an explicit method to register binary type based on class

Posted by tl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tledkov pushed a commit to branch ignite-15065
in repository https://gitbox.apache.org/repos/asf/ignite.git

commit 3281e53a499e7530d2f414692ff1b79951505055
Author: tledkov <tl...@gridgain.com>
AuthorDate: Wed Apr 7 11:29:18 2021 +0300

    IGNITE-15065 Add an explicit method to register binary type based on class
---
 .../main/java/org/apache/ignite/IgniteBinary.java  |   9 ++
 .../ignite/internal/client/thin/ClientBinary.java  |   8 +
 .../binary/CacheObjectBinaryProcessorImpl.java     |   8 +
 .../processors/cache/binary/IgniteBinaryImpl.java  |  12 ++
 .../cacheobject/IgniteCacheObjectProcessor.java    |  10 ++
 .../processors/cacheobject/NoOpBinary.java         |   5 +
 .../binary/BinaryMetadataRegisterClassTest.java    | 164 +++++++++++++++++++++
 .../testsuites/IgniteBinaryObjectsTestSuite.java   |   2 +
 8 files changed, 218 insertions(+)

diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteBinary.java b/modules/core/src/main/java/org/apache/ignite/IgniteBinary.java
index 53ee19d..18e6e91 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteBinary.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteBinary.java
@@ -408,4 +408,13 @@ public interface IgniteBinary {
      * @throws org.apache.ignite.binary.BinaryObjectException In case of error.
      */
     public BinaryType registerEnum(String typeName, Map<String, Integer> vals) throws BinaryObjectException;
+
+    /**
+     * Register binary type for specified class.
+     *
+     * @param cls Class.
+     * @return Metadata.
+     * @throws org.apache.ignite.binary.BinaryObjectException In case of error.
+     */
+    public BinaryType registerClass(Class<?> cls) throws BinaryObjectException;
 }
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientBinary.java b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientBinary.java
index 8f381a6..05d121a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientBinary.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientBinary.java
@@ -24,6 +24,7 @@ import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.binary.BinaryObjectBuilder;
 import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryType;
+import org.apache.ignite.internal.binary.BinaryClassDescriptor;
 import org.apache.ignite.internal.binary.BinaryContext;
 import org.apache.ignite.internal.binary.BinaryEnumObjectImpl;
 import org.apache.ignite.internal.binary.BinaryMetadata;
@@ -164,4 +165,11 @@ class ClientBinary implements IgniteBinary {
 
         return ctx.metadata(typeId);
     }
+
+    /** {@inheritDoc} */
+    @Override public BinaryType registerClass(Class<?> cls) throws BinaryObjectException {
+        BinaryClassDescriptor clsDesc = marsh.context().registerClass(cls, true, false);
+
+        return marsh.context().metadata(clsDesc.typeId());
+    }
 }
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
index 15619d3..0a94814 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
@@ -60,6 +60,7 @@ import org.apache.ignite.internal.IgniteInterruptedCheckedException;
 import org.apache.ignite.internal.IgniteNodeAttributes;
 import org.apache.ignite.internal.NodeStoppingException;
 import org.apache.ignite.internal.UnregisteredBinaryTypeException;
+import org.apache.ignite.internal.binary.BinaryClassDescriptor;
 import org.apache.ignite.internal.binary.BinaryContext;
 import org.apache.ignite.internal.binary.BinaryEnumObjectImpl;
 import org.apache.ignite.internal.binary.BinaryFieldMetadata;
@@ -1662,6 +1663,13 @@ public class CacheObjectBinaryProcessorImpl extends GridProcessorAdapter impleme
         }
     }
 
+    /** {@inheritDoc} */
+    @Override public BinaryType registerClass(Class<?> cls) throws BinaryObjectException {
+        BinaryClassDescriptor clsDesc = binaryCtx.registerClass(cls, true, false);
+
+        return metadata(clsDesc.typeId());
+    }
+
     /** */
     @SuppressWarnings("PublicInnerClass")
     public static class TestBinaryContext extends BinaryContext {
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/IgniteBinaryImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/IgniteBinaryImpl.java
index 2012b73..90c7c1b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/IgniteBinaryImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/IgniteBinaryImpl.java
@@ -179,6 +179,18 @@ public class IgniteBinaryImpl implements IgniteBinary {
         }
     }
 
+    /** {@inheritDoc} */
+    @Override public BinaryType registerClass(Class<?> cls) throws BinaryObjectException {
+        guard();
+
+        try {
+            return proc.registerClass(cls);
+        }
+        finally {
+            unguard();
+        }
+    }
+
     /**
      * @return Binary processor.
      */
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java
index 170a3c0..97dc6ec 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/IgniteCacheObjectProcessor.java
@@ -27,6 +27,7 @@ import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.binary.BinaryObjectBuilder;
+import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryType;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.internal.GridKernalContext;
@@ -358,4 +359,13 @@ public interface IgniteCacheObjectProcessor extends GridProcessor {
      * @param typeId Type ID.
      */
     public void removeType(int typeId);
+
+    /**
+     * Register binary type for specified class.
+     *
+     * @param cls Class.
+     * @return Metadata.
+     * @throws org.apache.ignite.binary.BinaryObjectException In case of error.
+     */
+    public BinaryType registerClass(Class<?> cls) throws BinaryObjectException;
 }
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/NoOpBinary.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/NoOpBinary.java
index 28ca35b..0cc2aab 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/NoOpBinary.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cacheobject/NoOpBinary.java
@@ -85,6 +85,11 @@ public class NoOpBinary implements IgniteBinary {
         throw unsupported();
     }
 
+    /** {@inheritDoc} */
+    @Override public BinaryType registerClass(Class<?> cls) throws BinaryObjectException {
+        throw unsupported();
+    }
+
     /** */
     private BinaryObjectException unsupported() {
         return new BinaryObjectException("Binary marshaller is not configured.");
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/BinaryMetadataRegisterClassTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/BinaryMetadataRegisterClassTest.java
new file mode 100644
index 0000000..b7e9b56
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/BinaryMetadataRegisterClassTest.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2021 GridGain Systems, Inc. and Contributors.
+ *
+ * Licensed under the GridGain Community Edition License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
+ *
+ * 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.processors.cache.binary;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteBinary;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.binary.BinaryObjectBuilder;
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryType;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class BinaryMetadataRegisterClassTest extends GridCommonAbstractTest {
+    /** */
+    private static final String CACHE_NAME = "cache";
+
+    /** Max retry cont for remove type. */
+    private static final int MAX_RETRY_CONT = 10;
+
+    /** */
+    private IgniteClient[] thinClients;
+
+    /** */
+    private List<IgniteBinary> testBinary;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setCacheConfiguration(new CacheConfiguration().setName(CACHE_NAME));
+
+        return cfg;
+    }
+
+    /** */
+    protected void startCluster() throws Exception {
+        startGrid("srv0");
+        startGrid("srv1");
+        startClientGrid("cli0");
+
+        thinClients = new IgniteClient[] {
+            Ignition.startClient(new ClientConfiguration().setAddresses("127.0.0.1:10800")),
+            Ignition.startClient(new ClientConfiguration().setAddresses("127.0.0.1:10800"))
+        };
+
+        testBinary = G.allGrids().stream().map(Ignite::binary).collect(Collectors.toList());
+
+        testBinary.add(thinClients[0].binary());
+        testBinary.add(0, thinClients[1].binary());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        startCluster();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+
+        for (IgniteClient thinClient : thinClients)
+            thinClient.close();
+
+        super.afterTest();
+    }
+
+    /** */
+    @Test
+    public void test() throws Exception {
+        for (IgniteBinary bin : testBinary) {
+            BinaryType binType = bin.registerClass(TestValue.class);
+
+            U.sleep(1000);
+
+            BinaryObjectBuilder bob = bin.builder(binType.typeName());
+            bob.setField("intField", "str value");
+
+            // Check that metadata is registered (the registered type of the 'intField' is Integer)
+            GridTestUtils.assertThrowsAnyCause(log,
+                () -> {
+                    bob.build();
+
+                    return null;
+                },
+                BinaryObjectException.class, "Wrong value has been set"
+            );
+
+            removeType(binType.typeName());
+        }
+    }
+
+    /**
+     * @param typeName Binary type name.
+     */
+    protected void removeType(String typeName) throws Exception {
+        IgniteEx ign = ((IgniteEx)F.first(G.allGrids()));
+
+        Exception err = null;
+
+        for (int i = 0; i < MAX_RETRY_CONT; ++i) {
+            try {
+                ign.context().cacheObjects().removeType(ign.context().cacheObjects().typeId(typeName));
+
+                err = null;
+
+                break;
+            }
+            catch (Exception e) {
+                err = e;
+
+                U.sleep(200);
+            }
+        }
+
+        if (err != null)
+            throw err;
+
+        U.sleep(1000);
+    }
+
+    /** */
+    public static class TestValue {
+        /** */
+        private int intField;
+
+        /** */
+        private double dblField;
+
+        /** */
+        private String strField;
+    }
+}
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java
index d882f24..b526357 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java
@@ -58,6 +58,7 @@ import org.apache.ignite.internal.binary.streams.BinaryOffheapStreamByteOrderSel
 import org.apache.ignite.internal.processors.cache.binary.BinaryAtomicCacheLocalEntriesSelfTest;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataInMemoryTest;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataMoveLegacyFolderTest;
+import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataRegisterClassTest;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataRegistrationCacheApiTest;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataRegistrationCacheStoreTest;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataRegistrationEntryProcessorTest;
@@ -93,6 +94,7 @@ import org.junit.runners.Suite;
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+    BinaryMetadataRegisterClassTest.class,
     BinaryMetadataRemoveTest.class,
     BinaryMetadataRemoveWithPersistenceTest.class,