From c836d4e4ffb177df0a63ca1500ca09184d543e8e Mon Sep 17 00:00:00 2001 From: denis-chudov Date: Mon, 10 Jun 2019 13:31:55 +0300 Subject: [PATCH] GG-19362 Fix flaky GridCountDownCallbackTest --- .../org/apache/ignite/IgniteScheduler.java | 2 +- .../GridCacheDatabaseSharedManager.java | 24 ++-- .../internal/util/GridCountDownCallback.java | 113 ------------------ .../util/GridCountDownCallbackTest.java | 68 ----------- .../testsuites/IgniteUtilSelfTestSuite.java | 5 +- 5 files changed, 14 insertions(+), 198 deletions(-) delete mode 100644 modules/core/src/main/java/org/apache/ignite/internal/util/GridCountDownCallback.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/util/GridCountDownCallbackTest.java diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteScheduler.java b/modules/core/src/main/java/org/apache/ignite/IgniteScheduler.java index 2ae2ad301f390..08a3ea84c4443 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteScheduler.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteScheduler.java @@ -108,4 +108,4 @@ public interface IgniteScheduler { * @return Scheduled execution future. */ public SchedulerFuture scheduleLocal(@NotNull Callable job, String ptrn); -} \ No newline at end of file +} diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java index eb7f835d0edf7..c4052df09a29c 100755 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java @@ -156,7 +156,6 @@ import org.apache.ignite.internal.processors.query.GridQueryProcessor; import org.apache.ignite.internal.metric.IoStatisticsHolderNoOp; import org.apache.ignite.internal.util.GridConcurrentHashSet; -import org.apache.ignite.internal.util.GridCountDownCallback; import org.apache.ignite.internal.util.GridMultiCollectionWrapper; import org.apache.ignite.internal.util.GridReadOnlyArrayView; import org.apache.ignite.internal.util.IgniteUtils; @@ -1463,13 +1462,9 @@ private void prepareIndexRebuildFuture(int cacheId) { @Override public void rebuildIndexesIfNeeded(GridDhtPartitionsExchangeFuture fut) { GridQueryProcessor qryProc = cctx.kernalContext().query(); - if (qryProc.moduleEnabled()) { - GridCountDownCallback rebuildIndexesCompleteCntr = new GridCountDownCallback( - cctx.cacheContexts().size(), - () -> log().info("Indexes rebuilding completed for all caches."), - 1 //need at least 1 index rebuilded to print message about rebuilding completion - ); + GridCompoundFuture compoundAllIdxsRebuilt = null; + if (qryProc.moduleEnabled()) { for (final GridCacheContext cacheCtx : (Collection)cctx.cacheContexts()) { if (cacheCtx.startTopologyVersion().equals(fut.initialVersion())) { final int cacheId = cacheCtx.cacheId(); @@ -1481,7 +1476,10 @@ private void prepareIndexRebuildFuture(int cacheId) { log().info("Started indexes rebuilding for cache [name=" + cacheCtx.name() + ", grpName=" + cacheCtx.group().name() + ']'); - assert usrFut != null : "Missing user future for cache: " + cacheCtx.name(); + if (compoundAllIdxsRebuilt == null) + compoundAllIdxsRebuilt = new GridCompoundFuture(); + + compoundAllIdxsRebuilt.add(rebuildFut); rebuildFut.listen(new CI1() { @Override public void apply(IgniteInternalFuture fut) { @@ -1503,8 +1501,6 @@ private void prepareIndexRebuildFuture(int cacheId) { + ", grpName=" + ccfg.getGroupName() + ']', err); } } - - rebuildIndexesCompleteCntr.countDown(true); } }); } @@ -1513,12 +1509,16 @@ private void prepareIndexRebuildFuture(int cacheId) { idxRebuildFuts.remove(cacheId, usrFut); usrFut.onDone(); - - rebuildIndexesCompleteCntr.countDown(false); } } } } + + if (compoundAllIdxsRebuilt != null) { + compoundAllIdxsRebuilt.listen(a -> log().info("Indexes rebuilding completed for all caches.")); + + compoundAllIdxsRebuilt.markInitialized(); + } } } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridCountDownCallback.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridCountDownCallback.java deleted file mode 100644 index 15ce491cd36cf..0000000000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/GridCountDownCallback.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2019 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.util; - -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Allows to execute callback when a set of operations will be completed. Also has an execution counter, - * which allows to block callback execution in case if it is below given threshold. By default, threshold - * is 0 and nothing blocks callback execution. - * - *

- * Sample usage:: - *

{@code
- *     GridCountDownCallback countDownCb = new GridCountDownCallback(
- *         n,                     //internal counter is initiated with n
- *         () -> doSomething()    //callback
- *     );
- *
- *     //each call of countDown() decrements internal counter
- *     //doSomething() will be executed after counter reaches 0
- *     for (int i = 0; i < n; i++)
- *         new Thread(() -> countDownCb.countDown()).start();
- * }
- * - *

- * Usage with execution threshold:: - *

{@code
- *     GridCountDownCallback countDownCb = new GridCountDownCallback(
- *         n,                     //internal counter is initiated with n
- *         () -> doSomething(),   //callback
- *         n/2                    //execution threshold is initiated with n/2
- *     );
- *
- *     //a half of calls of countDown() increase execution counter, so it reaches threshold and callback executes.
- *     //doSomething() will be executed after n threads will perform countDown()
- *     for (int i = 0; i < n; i++)
- *         new Thread(() -> countDownCb.countDown(n % 2 == 0)).start();
- * }
- */ -public class GridCountDownCallback { - /** */ - private final AtomicInteger cntr; - - /** */ - private final int executionThreshold; - - /** */ - private final AtomicInteger executionCntr = new AtomicInteger(0); - - /** */ - private final Runnable cb; - - /** - * Constructor. - * - * @param initCnt count of invocations of {@link #countDown}. - * @param cb callback which will be executed after initialCount - * invocations of {@link #countDown}. - * @param executionThreshold minimal count of really performed operations to execute callback. - */ - public GridCountDownCallback(int initCnt, Runnable cb, int executionThreshold) { - cntr = new AtomicInteger(initCnt); - - this.executionThreshold = executionThreshold; - - this.cb = cb; - } - - /** - * Constructor. Execution threshold is set to 0. - * - * @param initCnt count of invocations of {@link #countDown}. - * @param cb callback which will be executed after initialCount - * invocations of {@link #countDown}. - */ - public GridCountDownCallback(int initCnt, Runnable cb) { - this(initCnt, cb, 0); - } - - /** - * Decrements the internal counter. If counter becomes 0, callback will be executed. - * - * @param doIncreaseExecutionCounter whether to increase execution counter - */ - public void countDown(boolean doIncreaseExecutionCounter) { - if (doIncreaseExecutionCounter) - executionCntr.incrementAndGet(); - - if (cntr.decrementAndGet() == 0 && executionCntr.get() >= executionThreshold) - cb.run(); - } - - /** - * Decrements the internal counter. If counter becomes 0, callback will be executed. - */ - public void countDown() { - countDown(true); - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/GridCountDownCallbackTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/GridCountDownCallbackTest.java deleted file mode 100644 index 4839ac31eb550..0000000000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/util/GridCountDownCallbackTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2019 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.util; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** - * - */ -public class GridCountDownCallbackTest { - /** */ - @Test - public void testCountDownCallback() throws InterruptedException { - AtomicInteger cntr = new AtomicInteger(0); - AtomicInteger performedCntr = new AtomicInteger(0); - - AtomicBoolean res = new AtomicBoolean(); - - int countsTillCb = 30; - - GridCountDownCallback cb = new GridCountDownCallback( - countsTillCb, - () -> res.set(cntr.get() == countsTillCb && performedCntr.get() == countsTillCb / 5), - 0 - ); - - ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - - for (int i = 1; i < 100; i++) { - es.submit(() -> { - synchronized (es) { - int fi = cntr.incrementAndGet(); - - if (fi % 5 == 0) - performedCntr.incrementAndGet(); - - cb.countDown(fi % 5 == 0); - } - }); - } - - es.shutdown(); - - es.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); - - assertTrue(res.get()); - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java index 6612da572f1ad..b00e2f2537ef5 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java @@ -21,7 +21,6 @@ import org.apache.ignite.internal.pagemem.impl.PageIdUtilsSelfTest; import org.apache.ignite.internal.processors.cache.GridCacheUtilsSelfTest; import org.apache.ignite.internal.util.GridArraysSelfTest; -import org.apache.ignite.internal.util.GridCountDownCallbackTest; import org.apache.ignite.internal.util.IgniteDevOnlyLogTest; import org.apache.ignite.internal.util.IgniteExceptionRegistrySelfTest; import org.apache.ignite.internal.util.IgniteUtilsSelfTest; @@ -125,9 +124,7 @@ PageIdUtilsSelfTest.class, // control.sh - CommandHandlerParsingTest.class, - - GridCountDownCallbackTest.class + CommandHandlerParsingTest.class }) public class IgniteUtilSelfTestSuite { }