Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 89120200C52 for ; Mon, 10 Apr 2017 14:25:58 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 879EC160B99; Mon, 10 Apr 2017 12:25:58 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A89F0160B85 for ; Mon, 10 Apr 2017 14:25:57 +0200 (CEST) Received: (qmail 30488 invoked by uid 500); 10 Apr 2017 12:25:56 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 30479 invoked by uid 99); 10 Apr 2017 12:25:56 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Apr 2017 12:25:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B036EDFD9E; Mon, 10 Apr 2017 12:25:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: yzhdanov@apache.org To: commits@ignite.apache.org Message-Id: <2d95e7f9cfd641d9ab9739d64bdde7b0@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: ignite git commit: ignite-4828 - reviewed contribution Date: Mon, 10 Apr 2017 12:25:56 +0000 (UTC) archived-at: Mon, 10 Apr 2017 12:25:58 -0000 Repository: ignite Updated Branches: refs/heads/ignite-4828-reviewed [created] 6ccfb4384 ignite-4828 - reviewed contribution Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6ccfb438 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6ccfb438 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6ccfb438 Branch: refs/heads/ignite-4828-reviewed Commit: 6ccfb4384ea9ff5379cc84a9c7ea0c86e130538c Parents: 4425b40 Author: Yakov Zhdanov Authored: Mon Apr 10 15:22:47 2017 +0300 Committer: Yakov Zhdanov Committed: Mon Apr 10 15:22:47 2017 +0300 ---------------------------------------------------------------------- .../rendezvous/RendezvousAffinityFunction.java | 23 +++++++-- ...inityFunctionFastPowerOfTwoHashSelfTest.java | 50 ++++++++++++++++++++ ...ousAffinityFunctionStandardHashSelfTest.java | 50 ++++++++++++++++++++ 3 files changed, 120 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/6ccfb438/modules/core/src/main/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunction.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunction.java b/modules/core/src/main/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunction.java index 0fee1af..3845715 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunction.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunction.java @@ -101,6 +101,9 @@ public class RendezvousAffinityFunction implements AffinityFunction, Externaliza /** Number of partitions. */ private int parts; + /** Mask to use in calculation when partitions count is power of 2. */ + private int mask = -1; + /** Exclude neighbors flag. */ private boolean exclNeighbors; @@ -188,7 +191,9 @@ public class RendezvousAffinityFunction implements AffinityFunction, Externaliza A.ensure(parts > 0, "parts > 0"); this.exclNeighbors = exclNeighbors; - this.parts = parts; + + setPartitions(parts); + this.backupFilter = backupFilter; try { @@ -216,16 +221,22 @@ public class RendezvousAffinityFunction implements AffinityFunction, Externaliza } /** - * Sets total number of partitions. + * Sets total number of partitions.If the number of partitions is a power of two, + * the PowerOfTwo hashing method will be used. Otherwise the Standard hashing + * method will be applied. * * @param parts Total number of partitions. * @return {@code this} for chaining. */ public RendezvousAffinityFunction setPartitions(int parts) { - A.ensure(parts <= CacheConfiguration.MAX_PARTITIONS_COUNT, "parts <= " + CacheConfiguration.MAX_PARTITIONS_COUNT); + A.ensure(parts <= CacheConfiguration.MAX_PARTITIONS_COUNT, + "parts <= " + CacheConfiguration.MAX_PARTITIONS_COUNT); + A.ensure(parts > 0, "parts > 0"); this.parts = parts; + mask = (parts & (parts - 1)) == 0 ? parts - 1 : -1; + return this; } @@ -507,6 +518,12 @@ public class RendezvousAffinityFunction implements AffinityFunction, Externaliza throw new IllegalArgumentException("Null key is passed for a partition calculation. " + "Make sure that an affinity key that is used is initialized properly."); + if (mask >= 0) { + int h; + + return ((h = key.hashCode()) ^ (h >>> 16)) & mask; + } + return U.safeAbs(key.hashCode() % parts); } http://git-wip-us.apache.org/repos/asf/ignite/blob/6ccfb438/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest.java new file mode 100644 index 0000000..683ffa2 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest.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.cache.affinity.rendezvous; + +import org.apache.ignite.Ignite; +import org.apache.ignite.cache.affinity.AbstractAffinityFunctionSelfTest; +import org.apache.ignite.cache.affinity.AffinityFunction; +import org.apache.ignite.testframework.GridTestUtils; + +/** + * Tests for {@link RendezvousAffinityFunction}. + */ +public class RendezvousAffinityFunctionFastPowerOfTwoHashSelfTest extends AbstractAffinityFunctionSelfTest { + /** Ignite. */ + private static Ignite ignite; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + ignite = startGrid(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected AffinityFunction affinityFunction() { + AffinityFunction aff = new RendezvousAffinityFunction(512, null); + + GridTestUtils.setFieldValue(aff, "ignite", ignite); + + return aff; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6ccfb438/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionStandardHashSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionStandardHashSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionStandardHashSelfTest.java new file mode 100644 index 0000000..ed47c57 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/affinity/rendezvous/RendezvousAffinityFunctionStandardHashSelfTest.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.cache.affinity.rendezvous; + +import org.apache.ignite.Ignite; +import org.apache.ignite.cache.affinity.AbstractAffinityFunctionSelfTest; +import org.apache.ignite.cache.affinity.AffinityFunction; +import org.apache.ignite.testframework.GridTestUtils; + +/** + * Tests for {@link RendezvousAffinityFunction}. + */ +public class RendezvousAffinityFunctionStandardHashSelfTest extends AbstractAffinityFunctionSelfTest { + /** Ignite. */ + private static Ignite ignite; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + ignite = startGrid(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected AffinityFunction affinityFunction() { + AffinityFunction aff = new RendezvousAffinityFunction(513, null); + + GridTestUtils.setFieldValue(aff, "ignite", ignite); + + return aff; + } +}