This is an automated email from the ASF dual-hosted git repository.
nizhikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new d8cc5f44bbb IGNITE-29259 Resolve configured binary types with
configured class loader (#13799)
d8cc5f44bbb is described below
commit d8cc5f44bbbf7325284a718aeaa80ea172f1e477
Author: Nikolay <[email protected]>
AuthorDate: Tue Nov 10 12:29:50 2026 +0300
IGNITE-29259 Resolve configured binary types with configured class loader
(#13799)
---
.../ignite/internal/binary/BinaryContext.java | 27 ++++++----
...idCacheBinaryObjectUserClassloaderSelfTest.java | 57 ++++++++++++++++++++++
.../p2p/CacheDeploymentAffinityMappedKey.java | 50 +++++++++++++++++++
3 files changed, 124 insertions(+), 10 deletions(-)
diff --git
a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
index d5e08498535..c9ad35a2081 100644
---
a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
+++
b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
@@ -331,6 +331,20 @@ public class BinaryContext {
return clsLdr;
}
+ /**
+ * @param clsName Class name.
+ * @return Class loaded by the configured class loader or {@code null} if
it is not available to it.
+ * @see
org.apache.ignite.configuration.IgniteConfiguration#getClassLoader()
+ */
+ @Nullable private Class<?> loadClassOrNull(String clsName) {
+ try {
+ return CommonUtils.resolveClassLoader(null,
clsLdr).loadClass(clsName);
+ }
+ catch (ClassNotFoundException | NoClassDefFoundError ignored) {
+ return null;
+ }
+ }
+
/**
* @param globalNameMapper Name mapper.
* @param globalIdMapper ID mapper.
@@ -370,7 +384,7 @@ public class BinaryContext {
String affField = affFields.remove(clsName0);
if (affField == null) {
- Class<?> cls =
ClassLoaderUtils.classForName(clsName0);
+ Class<?> cls = loadClassOrNull(clsName0);
if (cls != null)
affField = affFldNameProvider.apply(cls);
@@ -384,7 +398,7 @@ public class BinaryContext {
String affField = affFields.remove(clsName);
if (affField == null) {
- Class<?> cls = ClassLoaderUtils.classForName(clsName);
+ Class<?> cls = loadClassOrNull(clsName);
if (cls != null)
affField = affFldNameProvider.apply(cls);
@@ -1115,14 +1129,7 @@ public class BinaryContext {
@Nullable Map<String, Integer> enumMap) throws BinaryObjectException {
assert mapper != null;
- Class<?> cls = null;
-
- try {
- cls = CommonUtils.resolveClassLoader(null,
classLoader()).loadClass(clsName);
- }
- catch (ClassNotFoundException | NoClassDefFoundError ignored) {
- // No-op.
- }
+ Class<?> cls = loadClassOrNull(clsName);
String typeName = mapper.typeName(clsName);
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridCacheBinaryObjectUserClassloaderSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridCacheBinaryObjectUserClassloaderSelfTest.java
index 8ceba782df8..f8f004bb813 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridCacheBinaryObjectUserClassloaderSelfTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/GridCacheBinaryObjectUserClassloaderSelfTest.java
@@ -18,10 +18,12 @@
package org.apache.ignite.internal.processors.cache.binary;
import java.io.Serializable;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
+import org.apache.ignite.binary.BinaryObject;
import org.apache.ignite.binary.BinaryObjectException;
import org.apache.ignite.binary.BinaryReader;
import org.apache.ignite.binary.BinarySerializer;
@@ -30,6 +32,7 @@ import org.apache.ignite.binary.BinaryWriter;
import org.apache.ignite.configuration.BinaryConfiguration;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.marshaller.ClassLoaderUtils;
import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.Test;
@@ -41,9 +44,18 @@ import static
org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
*
*/
public class GridCacheBinaryObjectUserClassloaderSelfTest extends
GridCommonAbstractTest {
+ /** Key class with an {@code @AffinityKeyMapped} field. Available through
the external class loader only. */
+ private static final String AFF_KEY_CLS =
"org.apache.ignite.tests.p2p.CacheDeploymentAffinityMappedKey";
+
+ /** Name of the field annotated with {@code @AffinityKeyMapped} in {@link
#AFF_KEY_CLS}. */
+ private static final String AFF_FIELD = "aff";
+
/** */
private static volatile boolean customBinaryConf = false;
+ /** Whether to list {@link #AFF_KEY_CLS} in the binary type
configurations. */
+ private static volatile boolean affKeyTypeConf = false;
+
/** */
private static volatile boolean deserialized = false;
@@ -103,6 +115,10 @@ public class GridCacheBinaryObjectUserClassloaderSelfTest
extends GridCommonAbst
cfg.setBinaryConfiguration(bcfg);
}
+ else if (affKeyTypeConf) {
+ cfg.setBinaryConfiguration(new BinaryConfiguration()
+ .setTypeConfigurations(Collections.singletonList(new
BinaryTypeConfiguration(AFF_KEY_CLS))));
+ }
return cfg;
}
@@ -197,6 +213,47 @@ public class GridCacheBinaryObjectUserClassloaderSelfTest
extends GridCommonAbst
}
}
+ /** Type is not listed in the binary configuration: the class is examined
when an instance is marshalled. */
+ @Test
+ public void testAffinityKeyFieldOfUnconfiguredType() throws Exception {
+ affKeyTypeConf = false;
+
+ doTestAffinityKeyField();
+ }
+
+ /** Type is listed in the binary configuration: the class is examined on
node start. */
+ @Test
+ public void testAffinityKeyFieldOfConfiguredType() throws Exception {
+ affKeyTypeConf = true;
+
+ try {
+ doTestAffinityKeyField();
+ }
+ finally {
+ affKeyTypeConf = false;
+ }
+ }
+
+ /**
+ * Checks that the {@code @AffinityKeyMapped} field of {@link
#AFF_KEY_CLS} is detected although the class is
+ * visible through the configured class loader only.
+ */
+ private void doTestAffinityKeyField() throws Exception {
+ assertNull(ClassLoaderUtils.classForName(AFF_KEY_CLS));
+
+ Ignite ignite = startGrid(1);
+
+ if (affKeyTypeConf)
+ assertEquals(AFF_FIELD,
ignite.binary().type(AFF_KEY_CLS).affinityKeyFieldName());
+
+ Object key =
ignite.configuration().getClassLoader().loadClass(AFF_KEY_CLS).newInstance();
+
+ BinaryObject bo = ignite.binary().toBinary(key);
+
+ assertEquals(AFF_FIELD, bo.type().affinityKeyFieldName());
+ assertEquals(AFF_FIELD,
ignite.binary().type(AFF_KEY_CLS).affinityKeyFieldName());
+ }
+
/**
*
*/
diff --git
a/modules/extdata/p2p/src/main/java/org/apache/ignite/tests/p2p/CacheDeploymentAffinityMappedKey.java
b/modules/extdata/p2p/src/main/java/org/apache/ignite/tests/p2p/CacheDeploymentAffinityMappedKey.java
new file mode 100644
index 00000000000..3b0b2dc10f7
--- /dev/null
+++
b/modules/extdata/p2p/src/main/java/org/apache/ignite/tests/p2p/CacheDeploymentAffinityMappedKey.java
@@ -0,0 +1,50 @@
+/*
+ * 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.tests.p2p;
+
+import org.apache.ignite.cache.affinity.AffinityKeyMapped;
+
+/**
+ * Test key with an {@link AffinityKeyMapped} field. Available through an
external class loader only.
+ */
+public class CacheDeploymentAffinityMappedKey {
+ /** Affinity key. */
+ @AffinityKeyMapped
+ private int aff = 1;
+
+ /** Key id. */
+ private int id = 2;
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ CacheDeploymentAffinityMappedKey that =
(CacheDeploymentAffinityMappedKey)o;
+
+ return aff == that.aff && id == that.id;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return 31 * aff + id;
+ }
+}