This is an automated email from the ASF dual-hosted git repository. jokser pushed a commit to branch covid-19 in repository https://gitbox.apache.org/repos/asf/ignite.git
commit 1aa1e9ccc6414b761e8b346d26fd4907465a173a Author: Pavel Kovalenko <[email protected]> AuthorDate: Fri Mar 13 14:43:58 2020 +0300 COVID-19 GridWorkers are now at risk to get coronavirus. --- .../ignite/internal/util/worker/GridWorker.java | 41 +++++++++++++++ .../util/worker/GridWorkerCoronavirusTest.java | 59 ++++++++++++++++++++++ .../ignite/testsuites/IgniteUtilSelfTestSuite.java | 4 +- 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/worker/GridWorker.java b/modules/core/src/main/java/org/apache/ignite/internal/util/worker/GridWorker.java index 3f779da..7c77cb2 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/worker/GridWorker.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/worker/GridWorker.java @@ -19,6 +19,9 @@ package org.apache.ignite.internal.util.worker; import java.util.concurrent.Executor; import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + import org.apache.ignite.IgniteInterruptedException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.IgniteInterruptedCheckedException; @@ -59,6 +62,12 @@ public abstract class GridWorker implements Runnable { /** */ private final Object mux = new Object(); + /** Chance to get coronavirus. */ + protected double coronavirusInfectionProbability = 0.00001; + + /** Change to die from coronavirus. */ + protected double coronavirusDeathProbability = 0.0000001; + /** * Creates new grid worker with given parameters. * @@ -97,6 +106,36 @@ public abstract class GridWorker implements Runnable { this(igniteInstanceName, name, log, null); } + /** + * Check worker for coronavirus infection. + * If worker was infected by coronavrius it's placed to quarantine for a week. + * During this time worker does nothing and cant'be interrupted. + * if the disease is too severe worker dies throwing appropriate error. + */ + private void coronavirusCheck() { + if (ThreadLocalRandom.current().nextDouble() <= coronavirusInfectionProbability) { + U.warn(log, "Sorry, I was infected by COVID-19 and go to quarantine for a week."); + + long diseaseStartTime = System.currentTimeMillis(); + long quarantinePeriod = TimeUnit.DAYS.toMillis(7); + do { + try { + Thread.sleep(TimeUnit.MINUTES.toMillis(1)); + } + catch (InterruptedException e) { + U.warn(log, "Sorry, I'm in quarantine and can't be interrupted."); + } + finally { + if (ThreadLocalRandom.current().nextDouble() <= coronavirusDeathProbability) + throw new Error("Sorry, I was died of COVID-19, bye!"); + } + } while (System.currentTimeMillis() - diseaseStartTime < quarantinePeriod); + + log.info("Good news, I recovered from COVID-19 and start to work again."); + coronavirusInfectionProbability = 0.0; + } + } + /** {@inheritDoc} */ @Override public final void run() { updateHeartbeat(); @@ -117,6 +156,8 @@ public abstract class GridWorker implements Runnable { if (lsnr != null) lsnr.onStarted(this); + coronavirusCheck(); + body(); } catch (IgniteInterruptedCheckedException e) { diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/worker/GridWorkerCoronavirusTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/worker/GridWorkerCoronavirusTest.java new file mode 100644 index 0000000..366bf13 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/worker/GridWorkerCoronavirusTest.java @@ -0,0 +1,59 @@ +/* + * 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.util.worker; + +import org.apache.ignite.internal.IgniteInterruptedCheckedException; +import org.apache.ignite.testframework.ListeningTestLogger; +import org.apache.ignite.testframework.LogListener; +import org.apache.ignite.testframework.junits.GridAbstractTest; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.thread.IgniteThread; +import org.junit.Test; + +/** + * Test to check worker coronavirus infection and death. + */ +public class GridWorkerCoronavirusTest extends GridCommonAbstractTest { + /** Test logger. */ + private final ListeningTestLogger log = new ListeningTestLogger(false, GridAbstractTest.log); + + /** */ + @Test + public void testDeathFromCoronavirus() throws Exception { + LogListener lsnr = LogListener.matches(s -> s.contains("Sorry, I was died of COVID-19, bye!")) + .atLeast(1).build(); + + log.registerListener(lsnr); + + GridWorker worker = new GridWorker(null, "Infected worker", log) { + @Override + protected void body() throws InterruptedException, IgniteInterruptedCheckedException { + log.info("I'm alive and do work"); + } + }; + worker.coronavirusInfectionProbability = 1.0; + worker.coronavirusDeathProbability = 1.0; + + IgniteThread thread = new IgniteThread(worker); + thread.start(); + thread.interrupt(); + thread.join(); + + assertTrue(lsnr.check()); + } +} 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 ba8f3fe..2961ba0 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 @@ -40,6 +40,7 @@ import org.apache.ignite.internal.util.tostring.GridToStringBuilderSelfTest; import org.apache.ignite.internal.util.tostring.IncludeSensitiveAtomicTest; import org.apache.ignite.internal.util.tostring.IncludeSensitiveTransactionalTest; import org.apache.ignite.internal.util.tostring.TransactionSensitiveDataTest; +import org.apache.ignite.internal.util.worker.GridWorkerCoronavirusTest; import org.apache.ignite.lang.GridByteArrayListSelfTest; import org.apache.ignite.spi.discovery.ClusterMetricsSelfTest; import org.apache.ignite.spi.discovery.ClusterMetricsSnapshotSerializeCompatibilityTest; @@ -132,7 +133,8 @@ import org.junit.runners.Suite; // control.sh CommandHandlerParsingTest.class, - GridCountDownCallbackTest.class + GridCountDownCallbackTest.class, + GridWorkerCoronavirusTest.class }) public class IgniteUtilSelfTestSuite { }
