ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject incubator-ignite git commit: IGNITE-342 Internal keys isn't passed in user's key mapper .
Date Thu, 26 Feb 2015 15:30:15 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-342 [created] a69151bc2


IGNITE-342 Internal keys isn't passed in user's key mapper .


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a69151bc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a69151bc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a69151bc

Branch: refs/heads/ignite-342
Commit: a69151bc2e29213772ceb9e433fd46d933c80d8a
Parents: 9e1377f
Author: nikolay_tikhonov <ntikhonov@gridgain.com>
Authored: Thu Feb 26 18:28:07 2015 +0300
Committer: nikolay_tikhonov <ntikhonov@gridgain.com>
Committed: Thu Feb 26 18:30:17 2015 +0300

----------------------------------------------------------------------
 .../affinity/GridAffinityAssignmentCache.java   |  19 +-
 .../cache/GridCacheAffinityManager.java         |  13 +-
 .../GridCacheDefaultAffinityKeyMapper.java      |  22 +-
 ...acheAbstractUsersAffinityMapperSelfTest.java | 207 +++++++++++++++++++
 ...dCacheAtomicUsersAffinityMapperSelfTest.java |  45 ++++
 ...heReplicatedUsersAffinityMapperSelfTest.java |  45 ++++
 .../GridCacheTxUsersAffinityMapperSelfTest.java |  45 ++++
 7 files changed, 391 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
index 9c12a17..9bfcca0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java
@@ -59,6 +59,9 @@ public class GridAffinityAssignmentCache {
     /** Affinity mapper function. */
     private final CacheAffinityKeyMapper affMapper;
 
+    /** Default affinity mapper function. */
+    private final CacheAffinityKeyMapper dfltAffMapper;
+
     /** Affinity calculation results cache: topology version => partition => nodes.
*/
     private final ConcurrentMap<Long, GridAffinityAssignment> affCache;
 
@@ -84,6 +87,7 @@ public class GridAffinityAssignmentCache {
      * @param cacheName Cache name.
      * @param aff Affinity function.
      * @param affMapper Affinity key mapper.
+     * @param dfltAffMapper Default affinity key mapper.
      * @param backups Number of backups.
      */
     @SuppressWarnings("unchecked")
@@ -91,6 +95,7 @@ public class GridAffinityAssignmentCache {
         String cacheName,
         CacheAffinityFunction aff,
         CacheAffinityKeyMapper affMapper,
+        CacheAffinityKeyMapper dfltAffMapper,
         int backups)
     {
         this.ctx = ctx;
@@ -98,6 +103,7 @@ public class GridAffinityAssignmentCache {
         this.affMapper = affMapper;
         this.cacheName = cacheName;
         this.backups = backups;
+        this.dfltAffMapper = dfltAffMapper;
 
         log = ctx.logger(GridAffinityAssignmentCache.class);
 
@@ -305,7 +311,18 @@ public class GridAffinityAssignmentCache {
             }
         }
 
-        return aff.partition(affMapper.affinityKey(key));
+        return aff.partition(affinityKey(key));
+    }
+
+    /**
+     * If Key is {@link GridCacheInternal GridCacheInternal} entry when won't passed into
user's mapper and
+     * will use {@link GridCacheDefaultAffinityKeyMapper default}.
+     * 
+     * @param key Key.
+     * @return Affinity key.
+     */
+    private Object affinityKey(Object key) {
+        return (dfltAffMapper != null && key instanceof GridCacheInternal ? dfltAffMapper
: affMapper).affinityKey(key);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java
index d3510e4..0e0b26b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java
@@ -44,8 +44,17 @@ public class GridCacheAffinityManager<K, V> extends GridCacheManagerAdapter<K,
V
 
     /** {@inheritDoc} */
     @Override public void start0() throws IgniteCheckedException {
-        aff = new GridAffinityAssignmentCache(cctx, cctx.namex(), cctx.config().getAffinity(),
-            cctx.config().getAffinityMapper(), cctx.config().getBackups());
+        GridCacheDefaultAffinityKeyMapper dfltAffKeyMapper = null;
+
+        if (!cctx.config().getAffinityMapper().getClass().equals(GridCacheDefaultAffinityKeyMapper.class))
{
+            dfltAffKeyMapper = new GridCacheDefaultAffinityKeyMapper();
+            
+            dfltAffKeyMapper.setIgnite(cctx.grid());
+            dfltAffKeyMapper.setLog(cctx.logger(GridCacheDefaultAffinityKeyMapper.class));
+        }
+
+        aff = new GridAffinityAssignmentCache(cctx, cctx.namex(), cctx.config().getAffinity(),

+            cctx.config().getAffinityMapper(), dfltAffKeyMapper, cctx.config().getBackups());
 
         // Generate internal keys for partitions.
         int partCnt = partitions();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheDefaultAffinityKeyMapper.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheDefaultAffinityKeyMapper.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheDefaultAffinityKeyMapper.java
index 2b6bfc9..87ed28e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheDefaultAffinityKeyMapper.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheDefaultAffinityKeyMapper.java
@@ -45,7 +45,6 @@ public class GridCacheDefaultAffinityKeyMapper implements CacheAffinityKeyMapper
     private static final long serialVersionUID = 0L;
 
     /** Injected ignite instance. */
-    @IgniteInstanceResource
     private Ignite ignite;
 
     /** Reflection cache. */
@@ -75,7 +74,6 @@ public class GridCacheDefaultAffinityKeyMapper implements CacheAffinityKeyMapper
     );
 
     /** Logger. */
-    @LoggerResource
     private transient IgniteLogger log;
 
     /**
@@ -124,4 +122,24 @@ public class GridCacheDefaultAffinityKeyMapper implements CacheAffinityKeyMapper
     @Override public void reset() {
         // No-op.
     }
+
+    /**
+     * Sets logger.
+     *
+     * @param log Ignite logger.
+     */
+    @LoggerResource
+    public void setLog(IgniteLogger log) {
+        this.log = log;
+    }
+
+    /**
+     * Sets Ignite.
+     *
+     * @param ignite Ignite.
+     */
+    @IgniteInstanceResource
+    public void setIgnite(Ignite ignite) {
+        this.ignite = ignite;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractUsersAffinityMapperSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractUsersAffinityMapperSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractUsersAffinityMapperSelfTest.java
new file mode 100644
index 0000000..5b43779
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractUsersAffinityMapperSelfTest.java
@@ -0,0 +1,207 @@
+/*
+ * 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.processors.cache;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.cache.affinity.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.internal.util.*;
+import org.apache.ignite.lang.*;
+import org.apache.ignite.spi.discovery.tcp.*;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.*;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.io.*;
+
+/**
+ * Test affinity mapper.
+ */
+public abstract class GridCacheAbstractUsersAffinityMapperSelfTest extends GridCommonAbstractTest
{
+    /** */
+    private static final int KEY_CNT = 1000;
+    
+    /** */
+    private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
+
+    /** */
+    public static final CacheAffinityKeyMapper AFFINITY_MAPPER = new UsersAffinityKeyMapper();
+    
+    /** */
+    public GridCacheAbstractUsersAffinityMapperSelfTest() {
+        super(false /* doesn't start grid */);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+        
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception
{
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        CacheConfiguration cacheCfg = new CacheConfiguration();
+
+        cacheCfg.setName(null);
+        cacheCfg.setCacheMode(getCacheMode());
+        cacheCfg.setAtomicityMode(getAtomicMode());
+        cacheCfg.setDistributionMode(getDistributionMode());
+        cacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
+        cacheCfg.setPreloadMode(CachePreloadMode.SYNC);
+        cacheCfg.setAffinityMapper(AFFINITY_MAPPER);
+
+        cfg.setCacheConfiguration(cacheCfg);
+
+        TcpDiscoverySpi spi = new TcpDiscoverySpi();
+
+        spi.setIpFinder(IP_FINDER);
+
+        cfg.setDiscoverySpi(spi);
+
+        return cfg;
+    }
+
+    /**
+     * @return Distribution mode.
+     */
+    protected abstract CacheDistributionMode getDistributionMode();
+
+    /**
+     * @return Cache atomicity mode.
+     */
+    protected abstract CacheAtomicityMode getAtomicMode();
+
+    /**
+     * @return Cache mode.
+     */
+    protected abstract CacheMode getCacheMode();
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testAffinityMapper() throws Exception {
+        IgniteCache<Object, Object> cache = startGrid(0).jcache(null);
+        
+        for (int i = 0; i < KEY_CNT; i++) {
+            cache.put(String.valueOf(i), String.valueOf(i));
+            
+            cache.put(new TestAffinityKey(i, String.valueOf(i)), i);
+        }
+        
+        assertEquals(1, cache.get(new TestAffinityKey(1, "1")));
+
+        startGrid(1);
+
+        for (int i = 0; i < KEY_CNT; i++)
+            grid(i % 2).compute().affinityRun(null, new TestAffinityKey(1, "1"), new NoopClosure());
+    }
+
+    /**
+     * Test key for field annotation.
+     */
+    private static class TestAffinityKey implements Externalizable {
+        /** Key. */
+        private int key;
+
+        /** Affinity key. */
+        @CacheAffinityKeyMapped
+        private String affKey;
+
+        /**
+         * Constructor.
+         */
+        public TestAffinityKey() {
+        }
+
+        /**
+         * Constructor.
+         *
+         * @param key Key.
+         * @param affKey Affinity key.
+         */
+        TestAffinityKey(int key, String affKey) {
+            this.key = key;
+            this.affKey = affKey;
+        }
+
+        /**
+         * @return Key.
+         */
+        public int key() {
+            return key;
+        }
+
+        /**
+         * @return Affinity key.
+         */
+        public String affinityKey() {
+            return affKey;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            return o instanceof TestAffinityKey && key == ((TestAffinityKey)o).key();
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return key + affKey.hashCode();
+        }
+
+        /** {@inheritDoc} */
+        @Override public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeInt(key);
+            out.writeUTF(affKey);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
+            key = in.readInt();
+            affKey = in.readUTF();
+        }
+    }
+
+    /**
+     * Users affinity mapper.
+     */
+    private static class UsersAffinityKeyMapper extends GridCacheDefaultAffinityKeyMapper{
+        /** {@inheritDoc} */
+        @Override public Object affinityKey(Object key) {
+            GridArgumentCheck.notNull(key, "key");
+                
+            assertFalse("GridCacheInternal entry mustn't be passed in user's key mapper.",
+                key instanceof GridCacheInternal);
+
+            return super.affinityKey(key);
+        }
+    }
+
+    /**
+     * Noop closure.
+     */
+    private static class NoopClosure implements IgniteRunnable {
+        /** {@inheritDoc} */
+        @Override public void run() {
+            // No-op.
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAtomicUsersAffinityMapperSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAtomicUsersAffinityMapperSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAtomicUsersAffinityMapperSelfTest.java
new file mode 100644
index 0000000..8a80e35
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAtomicUsersAffinityMapperSelfTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.processors.cache;
+
+import org.apache.ignite.cache.*;
+
+/**
+ * Test affinity mapper.
+ */
+public class GridCacheAtomicUsersAffinityMapperSelfTest extends GridCacheAbstractUsersAffinityMapperSelfTest
{
+    /** */
+    public GridCacheAtomicUsersAffinityMapperSelfTest() {
+        super();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheDistributionMode getDistributionMode() {
+        return CacheDistributionMode.PARTITIONED_ONLY;
+    };
+
+    /** {@inheritDoc} */
+    @Override protected CacheAtomicityMode getAtomicMode() {
+        return CacheAtomicityMode.ATOMIC;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheMode getCacheMode() {
+        return CacheMode.PARTITIONED;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReplicatedUsersAffinityMapperSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReplicatedUsersAffinityMapperSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReplicatedUsersAffinityMapperSelfTest.java
new file mode 100644
index 0000000..47e5dc7
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReplicatedUsersAffinityMapperSelfTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.processors.cache;
+
+import org.apache.ignite.cache.*;
+
+/**
+ * Test affinity mapper.
+ */
+public class GridCacheReplicatedUsersAffinityMapperSelfTest extends GridCacheAbstractUsersAffinityMapperSelfTest
{
+    /** */
+    public GridCacheReplicatedUsersAffinityMapperSelfTest() {
+        super();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheDistributionMode getDistributionMode() {
+        return CacheDistributionMode.PARTITIONED_ONLY;
+    };
+
+    /** {@inheritDoc} */
+    @Override protected CacheAtomicityMode getAtomicMode() {
+        return CacheAtomicityMode.ATOMIC;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheMode getCacheMode() {
+        return CacheMode.REPLICATED;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a69151bc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTxUsersAffinityMapperSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTxUsersAffinityMapperSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTxUsersAffinityMapperSelfTest.java
new file mode 100644
index 0000000..61af04e
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTxUsersAffinityMapperSelfTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.processors.cache;
+
+import org.apache.ignite.cache.*;
+
+/**
+ * Test affinity mapper.
+ */
+public class GridCacheTxUsersAffinityMapperSelfTest extends GridCacheAbstractUsersAffinityMapperSelfTest
{
+    /** */
+    public GridCacheTxUsersAffinityMapperSelfTest() {
+        super();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheDistributionMode getDistributionMode() {
+        return CacheDistributionMode.PARTITIONED_ONLY;
+    };
+
+    /** {@inheritDoc} */
+    @Override protected CacheAtomicityMode getAtomicMode() {
+        return CacheAtomicityMode.TRANSACTIONAL;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheMode getCacheMode() {
+        return CacheMode.PARTITIONED;
+    }
+}


Mime
View raw message