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 8079a0c..8400594 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 @@ -775,6 +775,12 @@ public class CacheObjectBinaryProcessorImpl extends IgniteCacheObjectProcessorIm return super.toCacheKeyObject(ctx, obj, userObj, partition); if (obj instanceof KeyCacheObject) { + if (obj instanceof BinaryObjectImpl) { + // Need to create a copy because the key can be reused at the application layer after that (IGNITE-3505). + BinaryObjectImpl bObj = (BinaryObjectImpl)obj; + obj = new BinaryObjectImpl(bObj.context(), bObj.array(), bObj.start()); + } + ((KeyCacheObject)obj).partition(partition); return (KeyCacheObject)obj; diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java index e3e538b..be2ce9b 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java @@ -23,11 +23,15 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import junit.framework.TestCase; import org.apache.ignite.IgniteBinary; +import org.apache.ignite.IgniteCache; import org.apache.ignite.IgniteCheckedException; 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.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; import org.apache.ignite.configuration.BinaryConfiguration; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; @@ -63,6 +67,7 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import static org.apache.ignite.cache.CacheMode.PARTITIONED; import static org.apache.ignite.cache.CacheMode.REPLICATED; /** @@ -74,10 +79,12 @@ public class BinaryObjectBuilderAdditionalSelfTest extends GridCommonAbstractTes IgniteConfiguration cfg = super.getConfiguration(gridName); CacheConfiguration cacheCfg = new CacheConfiguration(); - cacheCfg.setCacheMode(REPLICATED); - cfg.setCacheConfiguration(cacheCfg); + CacheConfiguration cacheCfg2 = new CacheConfiguration("partitioned"); + cacheCfg2.setCacheMode(PARTITIONED); + + cfg.setCacheConfiguration(cacheCfg, cacheCfg2); BinaryConfiguration bCfg = new BinaryConfiguration(); @@ -1359,6 +1366,33 @@ public class BinaryObjectBuilderAdditionalSelfTest extends GridCommonAbstractTes } /** + * @throws Exception If failed. + */ + public void testSameBinaryKey() throws Exception { + IgniteCache replicatedCache = + jcache(0).withKeepBinary(); + + IgniteCache partitionedCache = + jcache(0, "partitioned").withKeepBinary(); + + BinaryObjectBuilder keyBuilder = ignite(0).binary().builder("keyType") + .setField("F1", "V1").hashCode("V1".hashCode()); + + BinaryObjectBuilder valBuilder = ignite(0).binary().builder("valueType") + .setField("F2", "V2") + .setField("F3", "V3"); + + BinaryObject key = keyBuilder.build(); + BinaryObject val = valBuilder.build(); + + replicatedCache.put(key, val); + partitionedCache.put(key, val); + + assertNotNull(replicatedCache.get(key)); + assertNotNull(partitionedCache.get(key)); + } + + /** * @param obj Object. * @return Object in binary format. */